C# 将字符串转换为其等效的 byte[]
此时大多数人会想“啊,我会发布这个..:”
byte[] dataB= System.Text.Encoding.ASCII.GetBytes(data);
但是..我遇到的问题是我需要字节的确切值,没有编码,只是每个字节的纯值。 例如,如果字符串的值是(0xFF32),我希望它也将其转换为{255,50}。 这样做的原因是我有一种文件格式,我试图读取它,它将 int 存储为字节保存它们,然后在程序打开时读取它们。
这就是我到目前为止所得到的:
...
dialog.InitialDirectory =
Environment.GetFolderPath(System.Environment.SpecialFolder.Desktop) +
"/Test";
dialog.Title="Open File";
if (dialog.ShowDialog(this) == DialogResult.OK)
{
StreamReader reader = new StreamReader(dialog.FileName);
string data = reader.ReadToEnd();
reader.Close();
byte[] fileC = System.Text.Encoding.ASCII.GetBytes(data);
File_Read(dialog.FileName,fileC);
}
...
所以当我尝试读取文件时,它会将 0xFF 的文件约定转换为 0x3F,因为 0xFF 大于 127 并且 0x3F 是一个 ?。
抱歉,如果我看起来有点混乱:)
谢谢, 迈克尔
At this point most people will be thinking "Ah ill post this..:"
byte[] dataB= System.Text.Encoding.ASCII.GetBytes(data);
However.. the problem I have is i need the exact value of the bytes with no encoding just the pure value for each byte. For example if the value of the string is (0xFF32) i want it to convert it too {255,50}. he reason for this is I have a file format I am attempting to read which stores int's as bytes saves them and then reads them when the program opens.
This is what I have so far:
...
dialog.InitialDirectory =
Environment.GetFolderPath(System.Environment.SpecialFolder.Desktop) +
"/Test";
dialog.Title="Open File";
if (dialog.ShowDialog(this) == DialogResult.OK)
{
StreamReader reader = new StreamReader(dialog.FileName);
string data = reader.ReadToEnd();
reader.Close();
byte[] fileC = System.Text.Encoding.ASCII.GetBytes(data);
File_Read(dialog.FileName,fileC);
}
...
So when I try and read the file it converts the file convents of say 0xFF into 0x3F because 0xFF is greater then 127 and 0x3F is a ?.
Sorry if i seem a bit confusing :)
Thanks,
Michael
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
问题出在你的开始方法上:
......
这有点像看着一幅油画并说:“我想要该图片的字节,没有编码。” 这没有道理。 文本与二进制数据不同。 一旦理解了这一点,就很容易找到问题的根源。 您真正想要的是字节数组形式的文件内容。 这很简单,因为文件是二进制数据! 如果它不是真正的文本,那么您首先就不应该将其作为文本来阅读。 幸运的是,.NET 使这变得非常简单:
The problem is with your approach to start with:
...
That's a bit like looking at an oil painting and saying, "I want the bytes for that picture, with no encoding." It doesn't make sense. Text isn't the same as binary data. Once you understand that, it's easy to get to the root of the problem. What you really want is the contents of a file as a byte array. That's easy, because files are binary data! You shouldn't be reading it as text in the first place if it isn't really text. Fortunately, .NET makes this really easy:
然后使用它:
它返回 .NET 字符串内部存储的字节。
但这一切都是 codswallop:字符串总是链接到特定的编码,并且没有办法绕过它。 例如,如果文件包含无效的 Unicode 代码序列(可能会发生)或通过规范化,上述操作将会失败。 由于您显然不需要想要
字符串
,所以不要读取它。 改为将文件读取为二进制数据。Then use this:
It returns the bytes as stored internally by .NET strings.
But all this is codswallop: A string is always linked to a particular encoding and there's no way around it. The above will fail e.g. if the file contains invalid Unicode code sequences (which may happen) or through normalization. Since you obviously don't want a
string
, don't read one. Read the file as binary data instead.//将字符串转换为字节数组
//将字节数组转换为字符串
//convert a string to a byte array
//convert a byte array to a string
使用BinaryReader。
Use a BinaryReader.
为什么要从字符串转换? 直接把文件内容读成字节不行吗?
Why convert from string at all? Couldn't you just read the contents of the file directly into bytes?
如果您需要字节,请使用 Stream!
你到底为什么要搞乱 TextReader?
编辑:
根据您的示例,您正在打开一个文件,因此只需使用 FileStream。
If you want bytes, use a Stream!
Why on earth are you messing with a TextReader?
EDIT:
As per your example, you are opening a file, so just use a FileStream.