Results 1 to 3 of 3
- 04-21-2009, 11:49 AM #1
Member
- Join Date
- Apr 2009
- Posts
- 2
- Rep Power
- 0
- 04-21-2009, 05:06 PM #2
Senior Member
- Join Date
- Nov 2008
- Posts
- 286
- Rep Power
- 5
Anil, the unsigned/signed distinction is only relevant when you're interpreting the bytes. Otherwise, a byte is just a byte. So, say you want to send the byte value 200, you just add (byte) 200 to the array of data you're sending. At the other end, to read the signed byte as though it were unsigned, just and with 0xff. So:
and then the receiver:Java Code:// sender byte[] dataToSend = ... dataToSend[0] = (byte) 200;
On the receiving end, because the byte type stores -128 to 127, we put the value into an int, so that values between 128 and 255 can be represented.Java Code:byte[] receivedData = ... int firstByte = receivedData[0] & 0xff;
You may also be interested in my article on the Java equivalent of 'unsigned'.Last edited by neilcoffey; 04-21-2009 at 05:08 PM. Reason: added ref
Neil Coffey
Javamex - Java tutorials and performance info
- 04-28-2009, 02:52 AM #3
Similar Threads
-
How to read first 9 bytes and write in...?
By aRTx in forum New To JavaReplies: 8Last Post: 03-26-2009, 02:54 PM -
reading in unsigned ints into a 2D array
By newToIt in forum New To JavaReplies: 9Last Post: 03-06-2009, 12:36 PM -
midi bytes
By willemjav in forum Advanced JavaReplies: 77Last Post: 07-29-2008, 03:10 PM -
Reading bytes from InputStream
By Java Tip in forum Java TipReplies: 0Last Post: 11-25-2007, 07:51 PM -
how to know the number of bytes
By gabriel in forum New To JavaReplies: 2Last Post: 08-06-2007, 05:13 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks