C# - 读取字节,它们是什么以及发生了什么。我期望二进制值,而不是十进制数

发布于 2024-10-22 05:02:38 字数 1032 浏览 1 评论 0原文

我成为一名程序员已有几年了,但我从来没有理解涉及字节的低级操作。然而它让我感兴趣,并且我想了解更多有关使用字节的信息。

在下面的代码中,我正在读取一个仅包含“嗨,那里”一词的文本文件。

        FileStream fileStream = new FileStream(@"C:\myfile.txt", FileMode.Open);

        byte[] mybyte = new byte[fileStream.Length];

        fileStream.Read(mybyte, 0, (int)fileStream.Length);

        foreach(byte b in mybyte)
            Console.Write(b);

        Console.ReadLine();

在本例中,mybyte 变量包含似乎表示 ASCII 十进制对应项的数值。然而,我认为字节代表位,而位又代表二进制值。当读取一个字节时,我希望看到像“0001010”这样的二进制值,而不是“104”,它是“h”的ascii字符。

在读取图像的情况下,当将图像读入字节数组时,我再次看到数组中的数字,并且从低级角度来看,我期望二进制值。我知道这些数字显然不会映射到 Ascii,但我很困惑为什么在读取字符串时它们会映射到 ascii 数字,而在读取图像流时它会执行其他操作(我实际上不确定这些数字代表什么)在读取图像的情况下)。

我知道理解字节数组中数字的含义并不重要,但它让我很感兴趣。

有人可以在从文本文件读取和读取二进制文件(即图像)时阐明 .net 框架中的字节吗?谢谢

此图像是保存从 myfile.txt 读取的文本“hi There”的字节数组 myfile.txt 文件的字节数组。值映射到“hi There” 该图像是一个保存图像流的字节数组 在此处输入图像描述

I've been a programmer for a few years now, but I've never had to understand low-level operations involving bytes. It interests me however, and I would like to understand more about working with bytes.

In the below code I'm reading a text file that contains only the words "hi there".

        FileStream fileStream = new FileStream(@"C:\myfile.txt", FileMode.Open);

        byte[] mybyte = new byte[fileStream.Length];

        fileStream.Read(mybyte, 0, (int)fileStream.Length);

        foreach(byte b in mybyte)
            Console.Write(b);

        Console.ReadLine();

In this case, the mybyte variable contains numeric values that appear to represent the ASCII decimal counterpart. However, I thougth bytes represent bits, which in turn represnt binary values. When reading a byte I would expect to see a binary value like '0001010', not '104' which is the ascii character for 'h'.

In the case of reading an image, when reading the image into a byte array I once again see numbers in the array, and from a low-level persepctive I would expect binary values. I know that these numbers obviously don't map to Ascii, but I'm confused why when reading a string they would map to ascii numbers and when reading an image stream it does something else (I'm not actually sure what the numbers represent in the case of reading an image).

I know understanding what the numbers mean in a byte array isn't critical, but it greatly interests me.

Could someone please shed a light on bytes in the .net framework when reading from a text file and when reading binary (i.e. image). Thank You

This image is the byte array holding the text "hi there" read from myfile.txt
Byte array of the myfile.txt file.  Values map to "hi there"
This image is a byte array holding an image stream
enter image description here

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

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

发布评论

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

评论(4

青衫儰鉨ミ守葔 2024-10-29 05:02:38

01101000 是值 104 的 8 位表示形式。由于 ac# 字节存储 8 位 (0-255),因此它显示为更易读的内容。打开Windows计算器并将视图更改为“Programmer”,然后将其设置为“Bin”。可能会让事情变得更清楚一些。

它不向您显示十进制数,它向您显示 c# 字节,一个从 0 到 255 的数字

01101000 is the 8 bit representation of the value 104. Since a c# byte stores 8bits (0-255) it is shown to you as something more readable. Open up the windows calculator and change the view to "Programmer", then set it to "Bin". Might clears things up a bit.

It is not showing you a decimal number, it is showing you a c# byte, a number from 0 to 255

夏至、离别 2024-10-29 05:02:38

字节实际上是一个 8 位整数,表示为 0 到 255 之间的整数 - 换句话说,以十进制表示法。您期望它以二进制表示法表示,但实际上它意味着同样的事情。我能说的是,这就是 Visual Studio 在本例中的表现方式,但可能有人可以透露更多细节。

图像文件只是一组连续的字节,同样,这里全部表示为十进制数字。

希望有帮助。

A byte is literally an 8-bit integer that is represented there as an integer from 0 to 255 - in other words, in decimal notation. You were expecting it to be represented in binary notation, but it actually would mean the same thing. As best I can say is that's just how Visual Studio in this case represents it but there may some more details someone can shed.

An image file is just a sequential set of bytes, again, all represented here as decimal numbers.

Hope that helps.

梦幻之岛 2024-10-29 05:02:38

一个字节由 8 位组成。这些可以用不同的方式写入,例如十进制值 (104)、二进制值 (1101000) 或头十进制值 (68)。它们的含义完全相同,只是值的不同表示。

这与 ASCII 字符无关。它们恰好也是一个字节长(准确地说是 7 位)。

A byte consists of 8 bits. Those can be written in different ways, for example as decimal value (104), as binary values (1101000) or as headecimal value (68). They all mean exactly the same, it are just different representations of the values.

This has nothing to do with ASCII-Characters. They just happen to be a byte long, too (7 bit, to be precise).

无畏 2024-10-29 05:02:38

当然,低级别的所有内容都将存储为二进制值的集合。您在调试器中看到的是它的十进制表示形式。由于二进制值没有任何意义,除非我们解释它们,因此在两种情况(字符串和图像)中,您在调试器中看到的十进制数也是如此。

例如,当您从文件流中读取一个字节,然后使用如下编码对其进行解析时:

FileStream fs = new FileStream(@"<Filename>", FileMode.Open, FileAccess.Read, FileShare.Read);
            byte[] bt = new byte[8];
            fs.Read(bt , 0, 1);
            string str = System.Text.ASCIIEncoding.ASCII.GetString(bt);

即使您从图像文件中读取,您也会得到一个 ASCII 字符。如果您将相同的图像文件流传递给 Image 类

Bitmap bmp = (Bitmap)Image.FromFile(@"<Filename>");

,并将该 bmp 分配给图片框,您将看到一个图像。

摘要:
您的解释器会给出 0 和 1 或十进制数字的含义。它们本身没有任何意义。

Of course, everything at low-level will be stored as collection of binary values. What you are seeing with debugger is it's decimal representation. As binary values don't mean anything unless we interpret them, the same thing with the decimal number your seeing with the debugger in both the cases (string and image).

For example, when your read a byte from filestream and then parse it with encoding like:

FileStream fs = new FileStream(@"<Filename>", FileMode.Open, FileAccess.Read, FileShare.Read);
            byte[] bt = new byte[8];
            fs.Read(bt , 0, 1);
            string str = System.Text.ASCIIEncoding.ASCII.GetString(bt);

You will get a ASCII character even if your reading from a image file. If you pass the same image filestream to a Image class like

Bitmap bmp = (Bitmap)Image.FromFile(@"<Filename>");

and assign this bmp to picture box, you will see a image.

Summary:
Your interpreters give the meaning to your 0's and 1's or your decimal numbers. By themselves they don't mean anything.

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