string类型转成byte[]

string和byte[]的转换byte[] byteArray = System.Text.Encoding.Default.GetBytes ( str );

反过来,byte[]转成string

string和byte[]的转换string str = System.Text.Encoding.Default.GetString ( byteArray );


其它编码方式的,如System.Text.UTF8EncodingSystem.Text.UnicodeEncoding class等;例如:

string类型转成ASCII byte[]:("01" 转成 byte[] = new byte[]{ 0x30, 0x31}

string和byte[]的转换byte[] byteArray = System.Text.Encoding.ASCII.GetBytes ( str );

ASCII byte[] 转成string:(byte[] = new byte[]{ 0x30, 0x31} 转成 "01"

string和byte[]的转换string str = System.Text.Encoding.ASCII.GetString ( byteArray );

 

有时候还有这样一些需求:

byte[] 转成原16进制格式的string,例如0xae00cf, 转换成 "ae00cf"new byte[]{ 0x30, 0x31}转成"3031":

string和byte[]的转换        public static string ToHexString ( byte[] bytes ) // 0xae00cf => "AE00CF "
        }


反过来,16进制格式的string 转成byte[],例如, "ae00cf"转换成0xae00cf,长度缩减一半;"3031" 转成new byte[]{ 0x30, 0x31}:

string和byte[]的转换        public static byte[] GetBytes(string hexString, out int discarded)
        }

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-01-21
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-02-25
  • 2021-06-17
猜你喜欢
  • 2021-10-03
  • 2021-07-02
  • 2021-09-14
  • 2022-12-23
相关资源
相似解决方案