This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Your answer is nearly in your question. If you )]wrap a byte array from the server in a ByteBuffer and set the correct byte order, you can get integers, characters and other types from arbitrary offsets in the buffer.
Joined: Aug 21, 2004
Thank you very much. But I have char array.
In order to convert the char to byte, I guess I need to first convert char to string and then use getByte method of the string.
However the char contains binary data (which might change due to conversion to String and from it). Am I right?
Unless the InputStreamReader is using the ISO-8859-1 encoding, there may not be enough information left in the character array to get the binary data back out. For example, the integer 1000, in little endian order, is the bytes [-24, 3, 0, 0]. If you convert that to characters encoded as UTF-8 via an InputStreamReader, then back via a String, you get [-17, -65, -67, 3, 0, 0].
Without knowing any more about your protocol, I think you will need to switch to reading to a byte array (or ByteBuffer) to read the 4-byte integers correctly.
Is the server sending the data in Unicode characters? Or is your read method converting the original input to characters? InputStreamReader is a bridge from byte streams to character streams. What is the format for the data coming from the server? Maybe you need to tailor your read routine to read the data in the same format it is sent in. [ July 30, 2008: Message edited by: Norm Radder ]