[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: Byte Arrays



It sounds like the problem is the conversion from byte to String when you read the elements out of the stream. Is this correct? Also, I'm not clear on how you are transmitting the ByteArrayOutputStream over communications line or if that is relevant.
 
I notice that you show the first element as a binary number. Is the goal to get a String with the value "00001100"?
 
Jim Worthington
-----Original Message-----
From: Gregory A. Lusk [mailto:glusk@mesasub.com]
Sent: Wednesday, January 08, 2003 11:42 AM
To: ajug-members@www.ajug.org
Subject: Byte Arrays

I need to encode bytes into a ByteArrayOutputStream.  The data in each byte must be exactly eight bits long without truncating the leading zeros.  For example, if I want to encode the integer value 12 (as an unsigned byte) I am doing the following:

 

ByteArrayOutputStream out = new ByteArrayOutputStream();

short num = 12;

out.write(num & 0xFF);

 

However, if I read the first element in the ByteArrayOutputStream into a string variable, the leading zeros are truncated, leaving only 1100.

 

The communication protocol that we are using expects a single octet with the leading zeros left in, or else the framing will be off.  How do I maintain the zero placeholders in each byte and keep them from being truncated?  Or am I missing something here?

 

Greg Lusk