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
0
votes
1 answer

Effect of endianness on network byte order when doing reinterpret_cast

I am trying to convert a string representation of an IP address to its byte representation and back. For simplicity let's just talk about IPv4 addresses since endianness would affect both kinds of IP addresses in the same way. Given a string in the…
huzzm
  • 489
  • 9
  • 24
0
votes
1 answer

NodeJS Reading Buffer Binary To Float

I have a large DAT file that holds custom byte information, And I've been tasked with converting the solution to JavaScript. It's to make the solution be more single-language and convert to serverless cloud computing. But, I've run into an issue…
Zach
  • 539
  • 1
  • 4
  • 22
0
votes
1 answer

Converting 16BitPCM to .wav, after switching endianness, the .wav file plays backwards

I am trying to build an Android app that records PCM audio and exports it as a wav file. It worked fine for 8BitPCM, but when I switched to 16BitPCM I got white noise. I finally figured out it was the endianness of the byte array, but now, after…
stringgy
  • 85
  • 1
  • 1
  • 10
0
votes
1 answer

Implement fread (readInt) in java

I am attempting to convert a program that reads a binary file in C++ to java. The file is in little-endian. fread(n, sizeof (unsigned), 1, inputFile); The snippet above of c++ reads 1 integer into the integer variable 'n'. I am currently using this…
219CID
  • 340
  • 5
  • 15
0
votes
1 answer

How endianness swaps the order of elements, when converting two elements of uint32_t array to a single uint64_t?

I am confused as to how, endianness swaps the ordering of elements of a uint32_t array when converted to a uint64_t and vice-versa. If I'm dealing with bytes stored in a uint8_t array of 8 elements, if I directly convert it to a uint64_t using…
Vivekanand V
  • 340
  • 2
  • 12
0
votes
2 answers

Conversion to little endian format in C

I have an array int16_t arr[4]; . What I want to do is to convert a value in this array to little endian. For example, let us say I have 0x1d02 on the first index, but I need 0x21d there. Is there any elegant way of converting that and writing it…
Kalybor
  • 51
  • 8
0
votes
1 answer

Why does the raw parts not correspond to either endianness?

I'm trying to optimize as much as possible an operation done on slices of u32 from arrays of u8. As such, I'm testing different options (for loops, iterators, using ByteOrder crate, etc.) As part of these tests, I also wanted to check out if I could…
Dominus
  • 808
  • 11
  • 25
0
votes
1 answer

Confused by Pythons ctypes LittleEndianStructure

I'm experimenting with using the ctypes module to parse packets captured using the socket module and I'm confused by what I see when using the LittleEndianStructure. Using Python 3.6.8 on a CentOS 7 64bit VM, I'm capturing a UDP packet with the…
Adrian
  • 121
  • 1
  • 1
  • 11
0
votes
1 answer

Random bit double questions

I saw the following line in GTMHTTPFetcher.m of gtm-http-request: // set min interval to a random value between 1.0 and 2.0 seconds minRetryInterval_ = 1.0 + ((double)(arc4random() & 0x0FFFF) / (double) 0x0FFFF); Why are both operands of the…
ma11hew28
  • 121,420
  • 116
  • 450
  • 651
0
votes
2 answers

Copy two elements from uint8_t array into uint16_t variable using pointers

I have a uint8_t array. I sometimes need to treat two sequential elements as uint16_t, and copy them into another uint16_t variable. The elements may not necessarily start on a uint16_t word boundary. At present I'm using memcpy to do so: uint8_t…
retrodev
  • 2,323
  • 6
  • 24
  • 48
0
votes
2 answers

Can anyone help me with this exercise that i found online but i can't understand why the result is "aaaaaaaa". can you give me a hand?

i found this exercise online but i can't understand why the result is "aaaaaaaa". can you give me a hand ? #include void a(char * s) { while(*s++ != '\0') printf("a"); } int main() { int data[5] = {-1,-3,256,-4,0}; …
lory0312
  • 21
  • 1
0
votes
1 answer

template constexpr endian converter (without UB)

I have seen some other answers that propose to use unions for byte swapping (which is UB or can't be done at compile time). I've wrote mine, and it kind of worked until I have met some case which have shown that my implementation is invalid. I can't…
Sergey Kolesnik
  • 3,009
  • 1
  • 8
  • 28
0
votes
1 answer

Convert binary to signed, little endian 16bit integer in Python

Trying to a convert a binary list into a signed 16bit little endian integer input_data = [['1100110111111011','1101111011111111','0010101000000011'],['1100111111111011','1101100111111111','0010110100000011']] Desired Output =[[-1074, -34,…
red_sach
  • 47
  • 1
  • 8
0
votes
1 answer

c++ function to merge bytes of 2 values

I want to be able to merge bytes from two unsigned long parameters, taking exactly half of the bytes, the half that starts with the least significant byte of the second param and the rest of the first param. For example: x = 0x89ABCDEF12893456 y =…
Dima Ciun
  • 87
  • 1
  • 11
0
votes
0 answers

Changing endianess of a large chunk of memory

I've got 64 bytes of continuous memory organized into big-endian DWORDs. I'd like to copy this data and and organize as little-endian elsewhere. I could do that DWORD after DWORD, e. g.: mov eax, [rsi + rcx] bswap eax ; setting proper…
mdx
  • 534
  • 3
  • 16
1 2 3
99
100