从 byte[] 转换为字符串

发布于 2024-08-02 15:58:51 字数 1146 浏览 11 评论 0原文

我有以下代码:

using (BinaryReader br = new BinaryReader(
       File.Open(FILE_PATH, FileMode.Open, FileAccess.ReadWrite)))
{
    int pos = 0;
    int length = (int) br.BaseStream.Length;

    while (pos < length)
    {
        b[pos] = br.ReadByte();
        pos++;
    }

    pos = 0;
    while (pos < length)
    {
        Console.WriteLine(Convert.ToString(b[pos]));
        pos++;
    }
}

FILE_PATH 是一个常量字符串,其中包含正在读取的二进制文件的路径。 二进制文件是整数和字符的混合。 每个整数为 1 个字节,每个字符以 2 个字节写入文件。

例如,该文件具有以下数据:

1HELLO HOW ARE YOU45YOU ARE LOOKING GREAT //等等

请注意:每个整数都与其后面的字符串相关联。因此 1 与“HELLO HOW ARE YOU”相关,45 与“YOU ARE LOOKING GREAT”相关,依此类推。

现在二进制文件已写入(我不知道为什么,但我必须忍受这一点),使得“1”仅占用 1 个字节,而“H”(和其他字符)各占用 2 个字节。

所以这是文件实际包含的内容:

0100480045..等等 细分如下:

01 是整数 1 的第一个字节 0048 是 'H' 的 2 个字节(H 是十六进制的 48) 0045 是 'E' 的 2 个字节 (E = 0x45)

等等。 我希望我的控制台从此文件中打印出人类可读的格式:我希望它打印“1 HELLO HOW ARE YOU”,然后“45 YOU ARE LOOKING GREAT”等等......

我所做的正确吗?有更简单/有效的方法吗? 我的行 Console.WriteLine(Convert.ToString(b[pos]));只是打印整数值而不是我想要的实际字符。文件中的整数是可以的,但是如何读出字符呢?

任何帮助将不胜感激。 谢谢

I have the following code:

using (BinaryReader br = new BinaryReader(
       File.Open(FILE_PATH, FileMode.Open, FileAccess.ReadWrite)))
{
    int pos = 0;
    int length = (int) br.BaseStream.Length;

    while (pos < length)
    {
        b[pos] = br.ReadByte();
        pos++;
    }

    pos = 0;
    while (pos < length)
    {
        Console.WriteLine(Convert.ToString(b[pos]));
        pos++;
    }
}

The FILE_PATH is a const string that contains the path to the binary file being read.
The binary file is a mixture of integers and characters.
The integers are 1 bytes each and each character is written to the file as 2 bytes.

For example, the file has the following data :

1HELLO HOW ARE YOU45YOU ARE LOOKING GREAT //and so on

Please note: Each integer is associated with the string of characters following it. So 1 is associated with "HELLO HOW ARE YOU" and 45 with "YOU ARE LOOKING GREAT" and so on.

Now the binary is written (I do not know why but I have to live with this) such that '1' will take only 1 byte while 'H' (and other characters) take 2 bytes each.

So here is what the file actually contains:

0100480045..and so on
Heres the breakdown:

01 is the first byte for the integer 1
0048 are the 2 bytes for 'H' (H is 48 in Hex)
0045 are the 2 bytes for 'E' (E = 0x45)

and so on..
I want my Console to print human readable format out of this file: That I want it to print "1 HELLO HOW ARE YOU" and then "45 YOU ARE LOOKING GREAT" and so on...

Is what I am doing correct? Is there an easier/efficient way?
My line Console.WriteLine(Convert.ToString(b[pos])); does nothing but prints the integer value and not the actual character I want. It is OK for integers in the file but then how do I read out characters?

Any help would be much appreciated.
Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

久伴你 2024-08-09 15:58:52

我认为您正在寻找的是 Encoding.GetString

由于您的字符串数据由 2 个字节字符组成,因此如何获取字符串是:

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++;
  }
}

I think what you are looking for is Encoding.GetString.

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++;
  }
}
画中仙 2024-08-09 15:58:52

您可以使用 String System.Text.UnicodeEncoding.GetString() ,它接受一个 byte[] 数组并生成一个字符串。

我发现此链接非常有用

请注意,这与盲目地将字节从 byte[] 数组复制到一大块内存中并将其称为字符串不同。例如,GetString() 方法必须验证字节并禁止无效代理。

You can use String System.Text.UnicodeEncoding.GetString() which takes a byte[] array and produces a string.

I found this link very useful

Note that this is not the same as just blindly copying the bytes from the byte[] array into a hunk of memory and calling it a string. The GetString() method must validate the bytes and forbid invalid surrogates, for example.

失去的东西太少 2024-08-09 15:58:52
using (BinaryReader br = new BinaryReader(File.Open(FILE_PATH, FileMode.Open, FileAccess.ReadWrite)))
{    
   int length = (int)br.BaseStream.Length;    

   byte[] buffer = new byte[length * 2];
   int bufferPosition = 0;

   while (pos < length)    
   {        
       byte b = br.ReadByte();        
       if(b < 10)
       {
          buffer[bufferPosition] = 0;
          buffer[bufferPosition + 1] = b + 0x30;
          pos++;
       }
       else
       {
          buffer[bufferPosition] = b;
          buffer[bufferPosition + 1] = br.ReadByte();
          pos += 2;
       }
       bufferPosition += 2;       
   }    

   Console.WriteLine(System.Text.Encoding.Unicode.GetString(buffer, 0, bufferPosition));

}

using (BinaryReader br = new BinaryReader(File.Open(FILE_PATH, FileMode.Open, FileAccess.ReadWrite)))
{    
   int length = (int)br.BaseStream.Length;    

   byte[] buffer = new byte[length * 2];
   int bufferPosition = 0;

   while (pos < length)    
   {        
       byte b = br.ReadByte();        
       if(b < 10)
       {
          buffer[bufferPosition] = 0;
          buffer[bufferPosition + 1] = b + 0x30;
          pos++;
       }
       else
       {
          buffer[bufferPosition] = b;
          buffer[bufferPosition + 1] = br.ReadByte();
          pos += 2;
       }
       bufferPosition += 2;       
   }    

   Console.WriteLine(System.Text.Encoding.Unicode.GetString(buffer, 0, bufferPosition));

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文