Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Java read integers in little endian or big endian?

I ask because I am sending a byte stream from a C process to Java. On the C side the 32 bit integer has the LSB is the first byte and MSB is the 4th byte.

So my question is: On the Java side when we read the byte as it was sent from the C process, what is endian on the Java side?

A follow-up question: If the endian on the Java side is not the same as the one sent, how can I convert between them?

like image 357
hhafez Avatar asked Dec 12 '08 10:12

hhafez


People also ask

Does Java use big-endian or little endian?

In Java, data is stored in big-endian format (also called network order). That is, all data is represented sequentially starting from the most significant bit to the least significant.

Why does Java use big-endian?

Everything in Java binary files is stored in big-endian order. This is sometimes called network order. This means that if you use only Java, all files are done the same way on all platforms: Mac, PC, UNIX, etc. You can freely exchange binary data electronically without any concerns about endian-ness.

Is JVM little endian?

Each JVM stores and uses data in-memory in a big-endian order (where high bytes come first) whether the underlying OS/Hardware is big-endian or little endian.

Is Python big or little endian?

Python's Endianness is same as the processor in which the Python interpreter is run. The socket module of Python provides functions to handle translations of integers of different sizes from Little Endian to Big Endian and vice versa.


1 Answers

Use the network byte order (big endian), which is the same as Java uses anyway. See man htons for the different translators in C.

like image 92
Egil Avatar answered Oct 05 '22 22:10

Egil