从 byte[] 转换为字符串
我有以下代码:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为您正在寻找的是 Encoding.GetString。
由于您的字符串数据由 2 个字节字符组成,因此如何获取字符串是:
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:
您可以使用 String System.Text.UnicodeEncoding.GetString() ,它接受一个 byte[] 数组并生成一个字符串。
我发现此链接非常有用
You can use String System.Text.UnicodeEncoding.GetString() which takes a byte[] array and produces a string.
I found this link very useful
}
}