发新帖

[C#/ASP.NET] byte[] string 转换

零下一度 2023-12-4 494
01,C# string类型转成byte[]:
Byte[] byteArray = System.Text.Encoding.Default.GetBytes ( str );
02, C# byte[]转成string:
stringstr = System.Text.Encoding.Default.GetString ( byteArray );
03,C# string类型转成ASCIIbyte[]:
("01"转成byte[] =newbyte[]{0x30,0x31})
byte[] byteArray = System.Text.Encoding.ASCII.GetBytes ( str );
04,C# ASCIIbyte[]转成string:
(byte[] = new byte[]{ 0x30, 0x31} 转成"01")
string str = System.Text.Encoding.ASCII.GetString ( byteArray );
05, C# byte[]转16进制格式string:
new byte[]{ 0x30, 0x31}转成"3031":
public static string ToHexString ( byte[] bytes ) // 0xae00cf => "AE00CF "
{
string hexString = string.Empty;
if ( bytes != null )
{               
StringBuilder strB = new StringBuilder ();
for ( int i = 0; i < bytes.Length; i++ )
{                   
strB.Append ( bytes[i].ToString ( "X2" ) );               
}               
hexString = strB.ToString ();           
}
return hexString;       
}



@博客园@一贴灵



最新回复 (0)
返回
零下一度
主题数
931
帖子数
0
注册排名
1