tags:

views:

8161

answers:

6

I ask because I am sending a byte stream from a C processto Java (through a priority middle ware). 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 betwen them?

thanks:)

+1  A: 

There are no unsigned integers in Java (all 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;
Jonas Elfström
just realised that :$ so how I am supposed to send this unsigned little endian to my java process to read it correctly?
hhafez
int i = (int) b
Jonas Elfström
whay I mean by the start is that lsb is at the start of the 4 bytes (it's a unsigned 32 bit int ) so I did mean least significant byte
hhafez
Also I'm converting from C -> Java not from Java -> C :)
hhafez
Could you explain what you mean by the operations above?
hhafez
+2  A: 

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.

Wouter Lievens
Care to comment on why you're voting me down?
Wouter Lievens
because even if I where to read each byte individually the endianess of the byte that is sent would be incorrect so I would need to convert it
hhafez
Endianness of a byte? What the hell is that? Words have are sensitive to endianness, individual bytes don't.
Wouter Lievens
@hhafez That is not true, bytes does not have endianess as far as we need to be concerned if you read byte by byte, you, the programmer are responsible for assigning the bytes to the proper place. That is exactly what DataInputStream does, it just assembles the bytes together in a big endian way under the hoods.
nos
+3  A: 

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

Egil
I'm not at my linux box now but is htons one of the standard libs?
hhafez
According to http://h30097.www3.hp.com/docs//base_doc/DOCUMENTATION/V51_HTML/MAN/MAN3/0383____.HTM its part of the standard c library, yes
Egil
I'll gives this a try next monday but it looks prommising
hhafez
htons is available almost everywhere, but it's not in ISO C.
MSalters
If you have to use something other than network byte order, then you either roll your own with bitwise operators or use the various versions of java.nio.Buffer
Darron
According to its man-page it's defined in POSIX.1, so it should be available pretty much everywhere. And I seem to remember using it in Win32, so it's not just on POSIX systems either.
Joachim Sauer
+2  A: 

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

Ilja Preuß
He can only do that if his protocol uses the same endianness.
Wouter Lievens
Quite true. Thanks for pointing it out.
Ilja Preuß
A: 

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.

Joachim Sauer
Herms
But if you do this, you still can't tell what endianess your JVM uses internally.
Darron
Yes, but even there you're not directly mapping.You are using arithmetic that does exactly what you tell it, there's no ambiguity.In C you could always cast a "byte*" to a "long*" and de-reference it. Then you'd have to care about endianess. In Java there's no direct, ambiguous way to do that.
Joachim Sauer
Ah, I see. You were talking about the cast, not the binary math. Yea, in that case you're right.
Herms
+1  A: 

It doesn't have to do with Java or C Little Endian and Big Endian are the property of the processor.

Sandeep More