且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从byte []转换为字符串

更新时间:2022-06-09 04:40:10

我想你要找的是的 Encoding.GetString

由于您的字符串数据由2字节字符,你怎么可以让你的字符串的是:

Since your string data is composed of 2 byte characters, how you can get your string out is:

for (int i = 0; i < b.Length; i++)
{
  byte curByte = b[i];

  // Assuming that the first byte of a 2-byte character sequence will be 0
  if (curByte != 0)
  { 
    // This is a 1 byte number
    Console.WriteLine(Convert.ToString(curByte));
  }
  else
  { 
    // This is a 2 byte character. Print it out.
    Console.WriteLine(Encoding.Unicode.GetString(b, i, 2));

    // We consumed the next character as well, no need to deal with it
    //  in the next round of the loop.
    i++;
  }
}