119

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?

Will
  • 413
  • 6
  • 23
daydreamer
  • 87,243
  • 191
  • 450
  • 722
  • I guess this makes sense; because if you read Java I/O classes; for example ByteArrayInputStream; ByteArrayOutputStream. Unless one understands ByteArray, how does one clarify on what ByteArrayInputStream, ByteArrayOutputStream means? – CuriousMind Sep 20 '15 at 08:06

3 Answers3

92

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.

Phil
  • 157,677
  • 23
  • 242
  • 245
  • 21
    To get extremely pedantic, a byte is not guaranteed to be 8 bits. It's certainly the de facto standard of today but historically it's not always been the case. http://en.wikipedia.org/wiki/Byte – JaredPar Oct 26 '10 at 00:45
  • 16
    @JaredPar: accurate but I think it'd be a bit overkill for a disclaimer to be needed every time someone states that a byte is 8 bits. – Dinah Oct 26 '10 at 00:55
  • 6
    @Dinah, I agree, that's why I added the pedantic disclaimer. I just happened to be looking at the relevant page today. – JaredPar Oct 26 '10 at 01:12
  • 7
    Can you please put some light on `The downside to this is that the entire file contents must be loaded into memory.`? – CodeYogi Oct 20 '15 at 11:49
  • What a great and entertaining answer! Does converting to a byte array automatically make the code less vulnerable? – J.S. Orris Jun 02 '17 at 00:31
  • 1
    @JeffOrris not sure what you mean by *"converting to a byte array"*. That's not something you would typically do. Also, *"less vulnerable"* to what? – Phil Jun 02 '17 at 00:42
  • Is it basically a base 256 representation? – Alaska May 11 '22 at 05:10
  • @Phil Converting to a byte array happens all the time. Whenever one language interfaces with native code (for instance: Java or Swift needing to commune with C/C++, that communication needs to take place using byte arrays (or byte buffers), since the C's *C*annot *C*omprehend *C*ommon, *C*onventional *C*haracters. So, regardless of the data type your weapon of choice passes IN, it's gonna hafta get recast into the primitives, and then accordingly DEcoded on the way back OUT again, too. – NerdyDeeds Mar 14 '23 at 15:46
68

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.

user433534
  • 1,043
  • 6
  • 8
12

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.

Miguel Sevilla
  • 466
  • 3
  • 11