C# 将字符串转换为其 byte[] 等效项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/542766/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 07:32:02  来源:igfitidea点击:

C# convert string into its byte[] equivalent

c#stringencodinghexbyte

提问by redorkulated

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.

但是..我遇到的问题是我需要字节的确切值,而没有编码只是每个字节的纯值。例如,如果字符串的值是 (0xFF32),我希望它也将它转换为 {255,50}。这样做的原因是我有一个文件格式,我试图读取哪些存储 int 的作为字节保存它们,然后在程序打开时读取它们。

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 ?.

因此,当我尝试读取文件时,它会将 0xFF 的文件转换为 0x3F,因为 0xFF 大于 127,而 0x3F 是 ?。

Sorry if i seem a bit confusing :)

对不起,如果我看起来有点困惑:)

Thanks, Michael

谢谢,迈克尔

采纳答案by Jon Skeet

The problem is with your approach to start with:

问题在于您的方法是:

I need the exact value of the bytes with no encoding

我需要没有编码的字节的确切值

...

...

For example if the value of the string is (0xFF32)

例如,如果字符串的值为 (0xFF32)

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 fileas 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 让这一切变得非常简单:

byte[] fileC = File.ReadAllBytes(dialog.FileName);

回答by Phaedrus

Use a BinaryReader.

使用BinaryReader

回答by Konrad Rudolph

However.. the problem I have is i need the exact value of the bytes with no encoding just the pure value for each byte.

但是..我遇到的问题是我需要字节的确切值,而没有编码只是每个字节的纯值。

Then use this:

然后使用这个:

byte[] dataB = System.Text.Encoding.Unicode.GetBytes(data);

It returns the bytes as stored internally by .NET strings.

它返回 .NET 字符串内部存储的字节。

But all this is codswallop: A string is alwayslinked 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 wanta string, don't read one. Read the file as binary data instead.

但所有这一切都是 codswallop:一个字符串总是链接到特定的编码,没有办法绕过它。例如,如果文件包含无效的 Unicode 代码序列(可能发生)或通过规范化,则上述操作将失败。既然你显然不想要一个string,就不要读一个。改为将文件作为二进制数据读取。

回答by leppie

If you want bytes, use a Stream!

如果您想要字节,请使用 Stream!

Why on earth are you messing with a TextReader?

你到底为什么要搞乱 TextReader?

EDIT:

编辑:

As per your example, you are opening a file, so just use a FileStream.

根据您的示例,您正在打开一个文件,因此只需使用 FileStream。

回答by Dan

Why convert from string at all? Couldn't you just read the contents of the file directly into bytes?

为什么要从字符串转换?你不能直接把文件的内容读成字节吗?

byte[] fileC = File.ReadAllBytes(dialog.FileName);

回答by Atif Mahmood

//convert a string to a byte array

//将字符串转换为字节数组

public static byte[] StrToByteArray(string str)
{
    System.Text.UTF8Encoding  encoding=new System.Text.UTF8Encoding();
    return encoding.GetBytes(str);
}

//convert a byte array to a string

//将字节数组转换为字符串

public string ByteArrayToStr(byte [] dBytes)
{
System.Text.UTF8Encoding enc = new System.Text.UTF8Encoding();
return enc.GetString(dBytes);
}