字节数组是什么意思?

发布于 2024-09-29 10:07:06 字数 96 浏览 8 评论 0原文

有人可以解释一下吗,我不太明白这个概念。

什么是字节数组?

我们何时何地在应用程序/程序中使用它?

使用字节数组有哪些优点和缺点?

Could someone please explain, I do not exactly get the concept.

What is a Byte Array?

Where and when we use it in applications/programs?

what are the advantages and disadvantages of using a byte array?

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

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

发布评论

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

评论(3

携君以终年 2024-10-06 10:07:06

一个字节是 8 位(二进制数据)。

字节数组是字节数组(同义反复 FTW!)。

您可以使用字节数组来存储二进制数据的集合,例如文件的内容。这样做的缺点是必须将整个文件内容加载到内存中。

对于大量二进制数据,如果您的语言支持,最好使用流数据类型。

A byte is 8 bits (binary data).

A byte array is an array of bytes (tautology FTW!).

You could use a byte array to store a collection of binary data, for example, the contents of a file. The downside to this is that the entire file contents must be loaded into memory.

For large amounts of binary data, it would be better to use a streaming data type if your language supports it.

春花秋月 2024-10-06 10:07:06

我假设您知道字节是什么。字节数组只是包含一组连续(并排)字节的内存区域,因此按顺序讨论它们是有意义的:第一个字节,第二个字节等。

就像字节可以编码不同类型一样和数据范围(从 0 到 255 的数字、从 -128 到 127 的数字、使用 ASCII 的单个字符,例如“a”或“%”、CPU 操作码),字节数组中的每个字节可以是以下任何一个,或者贡献一些多字节值,例如更大范围的数字(例如从0..65535开始的16位无符号整数)、国际字符集、文本字符串(“hello”)或部分/全部已编译的计算机程序。

字节数组的关键在于,它可以对存储在该部分内存中的每个 8 位值进行索引(快速)、精确、原始访问,并且您可以对这些字节进行操作以控制每一位。不好的事情是计算机只是将每个条目视为一个独立的 8 位数字 - 这可能是您的程序正在处理的内容,或者您​​可能更喜欢一些强大的数据类型,例如跟踪其自身长度并增长的字符串根据需要,或者一个浮点数,让您可以存储 3.14,而无需考虑按位表示。作为一种数据类型,在长数组开头附近插入或删除数据效率很低,因为需要对所有后续元素进行混洗以形成或填充创建/所需的间隙。

I assume you know what a byte is. A byte array is simply an area of memory containing a group of contiguous (side by side) bytes, such that it makes sense to talk about them in order: the first byte, the second byte etc..

Just as bytes can encode different types and ranges of data (numbers from 0 to 255, numbers from -128 to 127, single characters using ASCII e.g. 'a' or '%', CPU op-codes), each byte in a byte array may be any of these things, or contribute to some multi-byte values such as numbers with larger range (e.g. 16-bit unsigned int from 0..65535), international character sets, textual strings ("hello"), or part/all of a compiled computer programs.

The crucial thing about a byte array is that it gives indexed (fast), precise, raw access to each 8-bit value being stored in that part of memory, and you can operate on those bytes to control every single bit. The bad thing is the computer just treats every entry as an independent 8-bit number - which may be what your program is dealing with, or you may prefer some powerful data-type such as a string that keeps track of its own length and grows as necessary, or a floating point number that lets you store say 3.14 without thinking about the bit-wise representation. As a data type, it is inefficient to insert or remove data near the start of a long array, as all the subsequent elements need to be shuffled to make or fill the gap created/required.

不再见 2024-10-06 10:07:06

来自 维基百科

在计算机科学中,数组数据
结构体或简单的数组是一个数据
由集合组成的结构
元素(值或变量),
每个由一个或多个整数标识
索引,存储以便地址
每个元素都可以根据其计算
通过简单的数学索引元组
公式。

因此,当您说字节数组时,您指的是某个已定义长度(例如元素数量)的数组,其中包含字节(8 位)大小的元素的集合。

在 C# 中,字节数组可能如下所示:

byte[] bytes = { 3, 10, 8, 25 };

上面的示例定义了一个包含 4 个元素的数组,其中每个元素最多可达 字节长度。

From wikipedia:

In computer science, an array data
structure or simply array is a data
structure consisting of a collection
of elements (values or variables),
each identified by one or more integer
indices, stored so that the address of
each element can be computed from its
index tuple by a simple mathematical
formula.

So when you say byte array, you're referring to an array of some defined length (e.g. number of elements) that contains a collection of byte (8 bits) sized elements.

In C# a byte array could look like:

byte[] bytes = { 3, 10, 8, 25 };

The sample above defines an array of 4 elements, where each element can be up to a Byte in length.

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