.net 什么是 byte[] 数组?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3089004/
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
What is a byte[] array?
提问by dotnet-practitioner
What is a bytearray is in the context of .NET framework?
byte.NET 框架上下文中的数组是什么?
I am familiar with standard definitions like arrayand byteand very familiar with electronic engineering concepts such as Byte. But I fail to connect it in terms of computer science concepts. I see it used everywhere, and I use it with out really understanding it deeply.
我熟悉标准定义,例如array和byte并且非常熟悉 Byte 等电子工程概念。但我未能将其与计算机科学概念联系起来。我看到到处都在使用它,但我在没有真正深入理解它的情况下使用它。
回答by jjnguy
In .NET, a byteis basically a number from 0to 255(the numbers that can be represented by eight bits).
在 .NET 中,abyte基本上是一个数字 from 0to 255(可以用八位表示的数字)。
So, a bytearray is just an array of the numbers 0 - 255.
因此,byte数组只是数字 0 - 255 的数组。
At a lower level, an array is a contiguous block of memory, and a byte array is just a representation of that memory in 8-bit chunks.
在较低级别,数组是一个连续的内存块,而字节数组只是该内存在 8 位块中的表示。
回答by spoulson
A byte[]array is simply an array of raw data. For example, a file of size 2000 bytes can be loaded into a byte[]array of 2000 elements.
甲byte[]阵列仅仅是原始数据的数组。例如,一个大小为 2000 字节的文件可以加载到一个byte[]包含 2000 个元素的数组中。
回答by David R Tribble
Technically, all of memory is one giant array of bytes (up to 232addressable bytes in a 32-bit address space). In C# (and C, C++, Java, and many other languages), a byte array is simply a contiguous chunk of memory. Thus a byte[n]array is a block of nbytes.
从技术上讲,所有内存都是一个巨大的字节数组(在 32 位地址空间中最多 2 32 个可寻址字节)。在 C#(以及 C、C++、Java 和许多其他语言)中,字节数组只是一块连续的内存。因此,byte[n]数组是一个n字节块。
Byte arrays typically have no type other than "byte", which is simply an 8-bit data item.
字节数组通常除了“字节”之外没有其他类型,它只是一个 8 位数据项。
Byte arrays are generally used for low-level I/O, such as read/write buffers for files and networks, as graphics image buffers, and as "untyped" data streams.
字节数组通常用于低级 I/O,例如文件和网络的读/写缓冲区、图形图像缓冲区和“无类型”数据流。
Addendum
附录
Bytes are also known as octets, i.e., eight-bit values. Octets are the universal unit for data interchange between practically all computer and information systems in use today.
字节也称为八位字节,即八位值。八位字节是当今使用的几乎所有计算机和信息系统之间进行数据交换的通用单位。
Even systems and encodings that use something other than 8-bit values still use octets to read from, write to, and transfer data between those systems. For example, audio CD sound samples are encoded as a stereo pair of signed 16-bit values sampled at 44,100 Hz. When accessed as a flat file (e.g., as a .WAV file) or data stream, though, it appears as a sequence of octets.
即使使用 8 位值以外的其他值的系统和编码仍然使用八位字节在这些系统之间读取、写入和传输数据。例如,音频 CD 声音样本被编码为一对以 44,100 Hz 采样的带符号 16 位值的立体声。但是,当作为平面文件(例如,作为 .WAV 文件)或数据流访问时,它显示为八位字节序列。
In the context of programming languages, then, such a sound file could be stored in its raw form as a single byte array.
在编程语言的上下文中,这样的声音文件可以以其原始形式存储为单个字节数组。
回答by Erik Funkenbusch
A byte is 8 bits, and an array of byte, is an array of bytes... It really is that simple.
一个字节是 8 位,一个字节数组,就是一个字节数组……真的就是这么简单。
The thing to keep in mind is that char and byte are different. In old C style, a char and byte were basically the same thing. In .NET, characters are Unicodeand can be anywhere from 8-32 bits per character. This is where encoding comes into play. You can convert a string to a byte array, and you can convert a byte array into a string by using the Encodingclass.
要记住的是 char 和 byte 是不同的。在旧的 C 风格中,字符和字节基本上是一回事。在 .NET 中,字符是Unicode,每个字符可以是 8-32 位。这就是编码发挥作用的地方。可以将字符串转换为字节数组,也可以使用Encoding类将字节数组转换为字符串。
回答by Yann Ramin
It's an array of byte. It's binary data - unstructured (in terms of the language at that point in time - different than meaningless!) data which can be arbitrarily long.
它是一个数组byte。它是二进制数据 - 非结构化(就当时的语言而言 - 与无意义不同!)数据可以任意长。
Think of loading a picture from a file. You would read the file into a byte[]before working with the image.
想想从文件加载图片。byte[]在处理图像之前,您会将文件读入 a 。
回答by Lajos Arpad
Byte Array: An array which only has elements of Byte type. Byte: Positive integer number between 0 and 255, closed intervallum. A and B are two bytes.
字节数组:只有字节类型元素的数组。字节:0 到 255 之间的正整数,闭区间。A 和 B 是两个字节。
If C = A + B, then, mathematically, C = (A + B) modulo 256 If C = A - B, then, mathematically, C = (A - B) modulo 256
如果 C = A + B,则在数学上,C = (A + B) 模 256 如果 C = A - B,则在数学上,C = (A - B) 模 256
So, you could consider (and sometimes use) your Byte Array of n elements as a number in the radix of 256, with n digits.
因此,您可以考虑(有时使用)您的 n 个元素的字节数组作为基数为 256 的数字,具有 n 位数字。

