http://www.sufeinet.com/plugin.php?id=keke_group

苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

分布式系统框架(V2.0) 轻松承载百亿数据,千万流量!讨论专区 - 源码下载 - 官方教程

HttpHelper爬虫框架(V2.7-含.netcore) HttpHelper官方出品,爬虫框架讨论区 - 源码下载 - 在线测试和代码生成

HttpHelper爬虫类(V2.0) 开源的爬虫类,支持多种模式和属性 源码 - 代码生成器 - 讨论区 - 教程- 例子

查看: 10354|回复: 1

[总群] C#字符串toString()方法与byte[0].toString()方法问题

[复制链接]
发表于 2013-3-28 21:31:22 | 显示全部楼层 |阅读模式
七夜琉璃()  21:20:51
那有个是这样的[code=csharp] recvBytes[10].ToString("X2")[/code]
站长苏飞<>  21:21:12
那是十六进制
七夜琉璃()  21:21:34
ox才是16进制
这个字节数组放的就是十六进制的数字
站长苏飞<>  21:22:40
[code=csharp]ToString没有OX这一项吧,只有X[/code]
七夜琉璃()  21:22:57
我不知道呀
站长苏飞<  21:22:59
[code=csharp]/// <summary>
/// 把字节数组 转 16进制字符串
/// </summary>
/// <param name="bytes"></param>
/// <returns></returns>
public static string byteToHexStr(byte[] bytes)
{
    string returnStr = "";
    if (bytes != null)
    {
        for (int i = 0; i < bytes.Length; i++)
        {
            returnStr += bytes.ToString("X2");
        }
    }
    return returnStr;
} [/code]
站长苏飞<sufei.1013@163.com>  21:23:51
X2是十六进制这个没错,
七夜琉璃()  21:24:09
恩恩。。好
知道了。谢咯
站长苏飞

C

            
            

货币

            
            
            

2.5.ToString("C")

            
            

            

¥2.50



D

            

十进制数

            
            
           

25.ToString("D5")

            
            
           

00025

            
            

            

E

            
            
            

科学型

            
            
            
            

25000.ToString("E")

            
            
            
            

2.500000E+005

            
            

            

F

            
            
            

固定点

            
            
            
            

25.ToString("F2")

            
            
            
            

25.00

            
            

            

G

            
            
            

常规

            
            
            
            

2.5.ToString("G")

            
            
            
            

2.5

            
            

            

N

            
            
            

数字

            
            
            
            

2500000.ToString("N")

            
            
            
            

2,500,000.00

            
            

            

X

            
            
            

十六进制

            
            
            
            

255.ToString("X")

            
            
            
            

FF

            

formatCode 是可选的格式化代码字符串。(详细内容请搜索“格式化字符串”查看)
必须用“{”和“}”将格式与其他字符分开。如果恰好在格式中也要使用大括号,可以用连续的两个大括号表示一个大括号,即: “{{”或者“}}”。
常用格式举例:
[code=csharp]
(1) int i=12345;

this.textBox1.Text=i.ToString();

//结果 12345(this指当前对象,或叫当前类的实例)

this.textBox2.Text=i.ToString("d8");

//结果 00012345

(2) int i=123;

double j=123.45;

string s1=string.Format("the value is {0,7:d}",i);

string s2=string.Format("the value is {0,7:f3}",j);

this.textBox1.Text=s1 ;

//结果 the value is 123

this.textBox2.Text=s2;

//结果 the value is 123.450

(3)double i=12345.6789;

this.textBox1.Text=i.ToString("f2"); //结果 12345.68

this.textBox2.Text=i.ToString("f6");

//结果 12345.678900

(4)double i=12345.6789;

this.textBox1.Text=i.ToString("n"); //结果 12,345.68

this.textBox2.Text=i.ToString(“n4”); //结果 12,345.6789

(5)double i=0.126;

string s=string.Format("the value is {0:p}",i);

this.textBox1.Text=i.ToString("p"); //结果 12.6%

this.textBox2.Text=s; //结果 the value is 12.6%

(6) DateTime dt =new DateTime(2003,5,25);

this.textBox1.Text=dt.ToString("yy.M.d");

//结果 03.5.25

this.textBox2.Text=dt.ToString(“yyyy年M月”);

//结果 2003年5月

Convert.ToDateTime("2005/12/22 22:22:22").ToString("yyyy/MM/dd HH:mm:ss")
"2005/12/22 22:22:22"

(7) int i=123;

double j=123.45;

string s=string.Format("i:{0,-7},j:{1,7}",i,j);

//-7表示左对齐,占7位

this.textBox1.Text=s ;

//结果i:123 ,j: 123.45

Byte.ToString 方法 (String, IFormatProvider)的C#例子



下面的代码示例使用 ToString 方法的几个重载格式化 Byte 值。

// Example for the Byte.ToString( ) methods.
using System;
using System.Globalization;

class ByteToStringDemo
{
    static void RunToStringDemo( )
    {
        byte smallValue = 13;
        byte largeValue = 234;
           
        // Format the Byte values without and with format strings.
        Console.WriteLine( "/nIFormatProvider is not used:" );
        Console.WriteLine( "   {0,-20}{1,10}{2,10}",
            "No format string:", smallValue.ToString( ),
            largeValue.ToString( ) );
        Console.WriteLine( "   {0,-20}{1,10}{2,10}",
            "'X2' format string:", smallValue.ToString( "X2" ),
            largeValue.ToString( "X2" ) );
           
        // Get the NumberFormatInfo object from the
        // invariant culture.
        CultureInfo         culture = new CultureInfo( "" );
        NumberFormatInfo    numInfo = culture.NumberFormat;
           
        // Set the digit grouping to 1, set the digit separator
        // to underscore, and set decimal digits to 0.
        numInfo.NumberGroupSizes = new int[ ] { 1 };
        numInfo.NumberGroupSeparator = "_";
        numInfo.NumberDecimalDigits = 0;
           
        // Use the NumberFormatInfo object for an IFormatProvider.
        Console.WriteLine(
            "/nA NumberFormatInfo object with digit group " +
            "size = 1 and /ndigit separator " +
            "= '_' is used for the IFormatProvider:" );
        Console.WriteLine( "   {0,-20}{1,10}{2,10}",
            "No format string:", smallValue.ToString( numInfo ),
            largeValue.ToString( numInfo ) );
        Console.WriteLine( "   {0,-20}{1,10}{2,10}",
            "'N' format string:",
            smallValue.ToString( "N", numInfo ),
            largeValue.ToString( "N", numInfo ) );
    }
      
    static void Main( )
    {
        Console.WriteLine( "This example of/n" +
            "   Byte.ToString( ),/n" +
            "   Byte.ToString( String ),/n" +
            "   Byte.ToString( IFormatProvider ), and/n" +
            "   Byte.ToString( String, IFormatProvider )/n" +
            "generates the following output when formatting " +
            "Byte values /nwith combinations of format " +
            "strings and IFormatProvider." );
           
        RunToStringDemo( );
    }
}

/*
This example of
   Byte.ToString( ),
   Byte.ToString( String ),
   Byte.ToString( IFormatProvider ), and
   Byte.ToString( String, IFormatProvider )
generates the following output when formatting Byte values
with combinations of format strings and IFormatProvider.

IFormatProvider is not used:
   No format string:           13       234
   'X2' format string:         0D        EA

A NumberFormatInfo object with digit group size = 1 and
digit separator = '_' is used for the IFormatProvider:
   No format string:           13       234
   'N' format string:         1_3     2_3_4
*/[/code]
后面的2 3之类的一般表示的是小数据位
站长苏飞<sufei.1013@163.com>  21:26:09
OX是在这种情况下使用的
[code=csharp]//C# 进制转换(二进制、十六进制、十进制互转)
//由于二进制数在C#中无法直接表示,所以所有二进制数都用一个字符串来表示
//例如: 二进制: 1010 表示为 字符串:"1010"
int d = 10;
//十进制转二进制字符串
Console.WriteLine(Convert.ToString(d,2));
//输出: 1010
//十进制转十六进制字符串
Console.WriteLine(Convert.ToString(d,16));
//输出: a
//二进制字符串转十进制数
string bin = "1010";
Console.WriteLine(Convert.ToInt32(bin,2));
//输出: 10
//二进制字符串转十六进制数
string bin = "1010";
Console.WriteLine(string.Format("{0:x}",Convert.ToInt32(bin,2));
//输出: a
//十六进制转二进制字符串
Console.WriteLine(Convert.ToString(0xa,2));
//输出: 1010
//十六进制转十进制数
Console.WriteLine(Convert.ToString(0xa,10));
//输出: 10[/code]


1. 开通SVIP会员,免费下载本站所有源码,不限次数据,不限时间
2. 加官方QQ群,加官方微信群获取更多资源和帮助
3. 找站长苏飞做网站、商城、CRM、小程序、App、爬虫相关、项目外包等点这里
发表于 2013-10-31 21:24:44 | 显示全部楼层
我用的是WPF 为什么我参照你上面的代码bytes.ToString("X2"); 是系统提示

错误        1        “ToString”方法没有任何重载采用“1”个参数
您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

QQ|手机版|小黑屋|手机版|联系我们|关于我们|广告合作|苏飞论坛 ( 豫ICP备18043678号-2)

GMT+8, 2024-4-20 18:35

© 2014-2021

快速回复 返回顶部 返回列表