What is a byte
array is in the context of .NET framework?
I am familiar with standard definitions like array
and byte
and very fam
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.
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.
Think of loading a picture from a file. You would read the file into a byte[]
before working with the image.
A byte is 8 bits, and an array of byte, is an array of bytes... It really is that simple.
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 Unicode and 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 Encoding class.
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.
If C = A + B, then, mathematically, C = (A + B) modulo 256 If C = A - B, then, mathematically, C = (A - B) modulo 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.
Technically, all of memory is one giant array of bytes (up to 232 addressable 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 n
bytes.
Byte arrays typically have no type other than "byte", which is simply an 8-bit data item.
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.
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.
In the context of programming languages, then, such a sound file could be stored in its raw form as a single byte array.
In .NET, a byte
is basically a number from 0
to 255
(the numbers that can be represented by eight bits).
So, a byte
array is just an array of the numbers 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.