Questions tagged [endianness]

Endianness refers to how multi-byte values are stored in memory, sent between devices or stored on disk. "Big-Endian" values are stored with their most-significant byte first, and "Little-Endian" values are stored with their least-significant byte first. Other byte-orders are possible but very uncommon, and cannot be described this way.

Endianness is the organization and ordering of byte values in multi-byte words. There are two main forms of endianness: big-endian and little-endian.

Big endian (BE) means that the most significant bits are stored first (lowest address). It is similar to reading or saying the name of a decimal number in reading order.

Little endian (LE) means that the least significant bits are stored first. The bytes are stored in reverse of the big-endian format.

There are other forms of byte orderings, but they are rare. They may also be called mixed-endian.

Usage of endianness

When we talk about endian, we often refer to the endianness of an instruction architecture/CPU or the endianness of a file. The endianness of an architecture or a CPU is how the processor organizes the bits in a multi-byte word.

  • Motorola 68000 is a big-endian architecture. It stores multi-byte words in big-endian ordering.
  • Intel processors and the x86 architecture are little-endian.
  • MIPS can run in both big-endian and little-endian format, and you can select the endianness. MIPS is a Bi-endian format.

The endianness of a file indicates how the bytes of a multi-byte word is ordered in a given file (applies both to binary and text files). Sometimes, we indicate the endianness of a file by putting a byte-order mark (BOM) as the first byte of that file.

  • A big-endian UTF-16 text file with BOM would begin with the two bytes FE FF and have all the two-byte characters (each surrogate in a surrogate pair is also one character) be expressed in big endian.
  • A little-endian UTF-16 text file with BOM would begin with the two bytes FF FE and have all the two-byte characters be expressed in little endian.

Examples of endianness

A 32-bit signed int value, 12356789 is stored as four bytes in two's complement format.

  • In big endian, the value is stored as 07 5B CD 15 in hexadecimal notation.
  • In little endian, the value is stored as 15 CD 58 07 in hexadecmial notation.

A UTF-16 text file with BOM contains these characters: A 汉.

  • The BOM character has value U+FEFF. The emoji has Unicode value U+1F197 is expressed as two surrogate pairs, U+D83C U+DD97
  • In big endian, the characters are stored as FEFF 0041 0020 6C49 D83C DD97
  • In little endian, they are stored as FFFE 4100 2000 496C 3CD8 97DD

Read More

Related tags:

External links:

2109 questions
22
votes
4 answers

Endianness of integers in Python

I'm working on a program where I store some data in an integer and process it bitwise. For example, I might receive the number 48, which I will process bit-by-bit. In general the endianness of integers depends on the machine representation of…
Gordon Seidoh Worley
  • 7,839
  • 6
  • 45
  • 82
22
votes
7 answers

How to convert a Byte Array to an Int Array

I am reading a file by using: int len = (int)(new File(args[0]).length()); FileInputStream fis = new FileInputStream(args[0]); byte buf[] = new byte[len]; fis.read(buf); As I found here. Is it possible to convert byte array buf…
alessandro
  • 1,681
  • 10
  • 33
  • 54
21
votes
3 answers

Big endian or Little endian on net?

In what byte order does data transfer occur on net? Is it Little Endian or big endian? How is it converted to the respective byte order once the data reaches the host ?
TwiggedToday
  • 9,925
  • 7
  • 26
  • 16
21
votes
5 answers

Converting a UINT32 value into a UINT8 array[4]

My question is how do you convert a UINT32 value to a UINT8 array[4] (C/C++) preferably in a manner independent of endianness? Additionally, how would you reconstruct the UINT32 value from the UINT8 array[4], to get back to where you started?
Chris
  • 211
  • 1
  • 2
  • 3
21
votes
2 answers

What should I #include to use 'htonl'?

I want to use the htonl function in my ruby c extension, but don't want to use any of the other internet stuff that comes with it. What would be the most minimalistic file to #include that is still portable? Looking through the header files on my…
Adrian
  • 14,931
  • 9
  • 45
  • 70
20
votes
3 answers

Undefined behavior from pointer math on a C++ array

Why the output of this program is 4? #include int main() { short A[] = {1, 2, 3, 4, 5, 6}; std::cout << *(short*)((char*)A + 7) << std::endl; return 0; } From my understanding, on x86 little endian system, where char has 1…
Jacek Skiba
  • 251
  • 1
  • 8
19
votes
8 answers

How do I convert a big-endian struct to a little endian-struct?

I have a binary file that was created on a unix machine. It's just a bunch of records written one after another. The record is defined something like this: struct RECORD { UINT32 foo; UINT32 bar; CHAR fooword[11]; CHAR barword[11]; UNIT16…
scottm
  • 27,829
  • 22
  • 107
  • 159
19
votes
7 answers

C# Big-endian ulong from 4 bytes

Im trying to cast a 4 byte array to an ulong in C#. I'm currently using this code: atomSize = BitConverter.ToUInt32(buffer, 0); The byte[4] contains this: 0 0 0 32 However, the bytes are Big-Endian. Is there a simple way to convert this Big-Endian…
WesleyE
  • 1,384
  • 1
  • 12
  • 29
19
votes
4 answers

Large flags enumerations in C#

Hey everyone, got a quick question that I can't seem to find anything about... I'm working on a project that requires flag enumerations with a large number of flags (up to 40-ish), and I don't really feel like typing in the exact mask for each…
LorenVS
  • 12,597
  • 10
  • 47
  • 54
19
votes
6 answers

Converting a UINT16 value into a UINT8 array[2]

This question is basically the second half to my other Question How can I convert a UINT16 value, into a UINT8 * array without a loop and avoiding endian problems. Basically I want to do something like this: UINT16 value = 0xAAFF; UINT8 array[2] =…
dp.
  • 229
  • 1
  • 2
  • 11
19
votes
7 answers

PostGIS Geometry saving: "Invalid endian flag value encountered."

I have a Spring Roo + Hibernate project which takes a JTS well-known text (WKT) String input from the client application, converts it into a JTS Geometry object, and then attempts to write it to the PostGIS database. I had some problems with the…
orlade
  • 2,060
  • 4
  • 24
  • 35
18
votes
5 answers

Convert big endian to little endian when reading from a binary file

I've been looking around how to convert big-endian to little-endians. But I didn't find any good that could solve my problem. It seem to be there's many way you can do this conversion. Anyway this following code works ok in a big-endian system. But…
starcorn
  • 8,261
  • 23
  • 83
  • 124
18
votes
2 answers

create AudioClip from byte[]

I have problem. I use sqlite to store sounds. I get sound from it in byte[]. Then convert byte[] to float[]: private float[] ConvertByteToFloat(byte[] array) { float[] floatArr = new float[array.Length / 4]; …
Igor
  • 376
  • 1
  • 6
  • 14
18
votes
3 answers

(java) Writing in file little endian

I'm trying to write TIFF IFDs, and I'm looking for a simple way to do the following (this code obviously is wrong but it gets the idea across of what I want): out.writeChar(12) (bytes 0-1) out.writeChar(259) (bytes 2-3) out.writeChar(3) (bytes…
Tony Stark
  • 24,588
  • 41
  • 96
  • 113
17
votes
6 answers

find endianness of system in java

I found out that algorithm (int C) for checkink if machine is bigindian or littleindian is int is_big_endian(void) { union { uint32_t i; char c[4]; } bint = {0x01020304}; return bint.c[0] == 1; } How can i find such…
user93796
  • 18,749
  • 31
  • 94
  • 150