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

发布于 2024-07-13 09:47:18 字数 843 浏览 6 评论 0原文

此时大多数人会想“啊,我会发布这个..:”

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 技术交流群。

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

发布评论

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

评论(6

青芜 2024-07-20 09:47:18

问题出在你的开始方法上:

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

......

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

这有点像看着一幅油画并说:“我想要该图片的字节,没有编码。” 这没有道理。 文本与二进制数据不同。 一旦理解了这一点,就很容易找到问题的根源。 您真正想要的是字节数组形式的文件内容。 这很简单,因为文件是二进制数据! 如果它不是真正的文本,那么您首先就不应该将其作为文本来阅读。 幸运的是,.NET 使这变得非常简单:

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

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)

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:

byte[] fileC = File.ReadAllBytes(dialog.FileName);
几味少女 2024-07-20 09:47:18

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

然后使用它:

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

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

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

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.

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.

§普罗旺斯的薰衣草 2024-07-20 09:47:18

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

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

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

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

//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);
}
金橙橙 2024-07-20 09:47:18

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

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

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);
も星光 2024-07-20 09:47:18

如果您需要字节,请使用 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.

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