Does Java read integers in little endian or big endian?

JavaEndianness

Java Problem Overview


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?

Java Solutions


Solution 1 - Java

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

Solution 2 - Java

I stumbled here via Google and got my answer that Java is big endian.

Reading through the responses I'd like to point out that bytes do indeed have an endian order, although mercifully, if you've only dealt with “mainstream” microprocessors you are unlikely to have ever encountered it as Intel, Motorola, and Zilog all agreed on the shift direction of their UART chips and that MSB of a byte would be 2**7 and LSB would be 2**0 in their CPUs (I used the FORTRAN power notation to emphasize how old this stuff is :) ).

I ran into this issue with some Space Shuttle bit serial downlink data 20+ years ago when we replaced a $10K interface hardware with a Mac computer. There is a NASA Tech brief published about it long ago. I simply used a 256 element look up table with the bits reversed (table[0x01]=0x80 etc.) after each byte was shifted in from the bit stream.

Solution 3 - Java

There are no unsigned integers in Java. All integers are signed and in big endian.

> On the C side the each byte has tne LSB at the start is on the left and the MSB at the end.

It sounds like you are using LSB as Least significant bit, are you? LSB usually stands for least significant byte. Endianness is not bit based but byte based.

To convert from unsigned byte to a Java integer:

int i = (int) b & 0xFF;

To convert from unsigned 32-bit little-endian in byte[] to Java long (from the top of my head, not tested):

long l = (long)b[0] & 0xFF;
l += ((long)b[1] & 0xFF) << 8;
l += ((long)b[2] & 0xFF) << 16;
l += ((long)b[3] & 0xFF) << 24;

Solution 4 - Java

There's no way this could influence anything in Java, since there's no (direct non-API) way to map some bytes directly into an int in Java.

Every API that does this or something similar defines the behaviour pretty precisely, so you should look up the documentation of that API.

Solution 5 - Java

I would read the bytes one by one, and combine them into a long value. That way you control the endianness, and the communication process is transparent.

Solution 6 - Java

If it fits the protocol you use, consider using a DataInputStream, where the behavior is very well defined.

Solution 7 - Java

Java is 'Big-endian' as noted above. That means that the MSB of an int is on the left if you examine memory (on an Intel CPU at least). The sign bit is also in the MSB for all Java integer types.
Reading a 4 byte unsigned integer from a binary file stored by a 'Little-endian' system takes a bit of adaptation in Java. DataInputStream's readInt() expects Big-endian format.
Here's an example that reads a four byte unsigned value (as displayed by HexEdit as 01 00 00 00) into an integer with a value of 1:

 // Declare an array of 4 shorts to hold the four unsigned bytes
 short[] tempShort = new short[4];
 for (int b = 0; b < 4; b++) {
    tempShort[b] = (short)dIStream.readUnsignedByte();           
 }
 int curVal = convToInt(tempShort);

 // Pass an array of four shorts which convert from LSB first 
 public int convToInt(short[] sb)
 {
   int answer = sb[0];
   answer += sb[1] << 8;
   answer += sb[2] << 16;
   answer += sb[3] << 24;
   return answer;        
 }

Solution 8 - Java

Imho there is no endianness defined for java. The endianness is the one of the hardware but java is highlevel and hides the hardware so you don't have to wory about that.

The only endianess related feature is how the java lib maps int and long to byte[] (and inversely). It does it Big-Endian which is the most readable and natural:

int i=0xAABBCCDD

maps to

byte[] b={0xAA,0xBB,0xCC,0xDD}

Solution 9 - Java

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionhhafezView Question on Stackoverflow
Solution 1 - JavaEgilView Answer on Stackoverflow
Solution 2 - JavaWB GreeneView Answer on Stackoverflow
Solution 3 - JavaJonas ElfströmView Answer on Stackoverflow
Solution 4 - JavaJoachim SauerView Answer on Stackoverflow
Solution 5 - JavaWouter LievensView Answer on Stackoverflow
Solution 6 - JavaIlja PreußView Answer on Stackoverflow
Solution 7 - JavaDonald W. SmithView Answer on Stackoverflow
Solution 8 - JavaJavaddictView Answer on Stackoverflow
Solution 9 - Javauser12482548View Answer on Stackoverflow