Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > converting int and short to byte array and vice versa

Reply
Thread Tools

converting int and short to byte array and vice versa

 
 
carmen
Guest
Posts: n/a
 
      04-13-2005
Hi,

Could anyone suggest a way to convert a short to a 2 byte array and a
int to a 4 byte array and vice versa? Is there any way to do it using
utility classes provided by java?
I have just begun to code in java ,

regards,
Carmen

 
Reply With Quote
 
 
 
 
foo
Guest
Posts: n/a
 
      04-13-2005
// For int. for short is similar

/**
* Returns rightmost byte as the 0th element of the array
*/
bytesValue(int myInt) {
byte[] bytes = new byte[4];
hexBase = 0xff; // A byte of all ones
bytes[0] = hexBase & myInt;
bytes[1] = ((hexBase << & myInt) >> 8;
bytes[2] = ((hexBase << 16) & myInt) >> 16;
bytes[3] = ((hexBase << 24) & myInt) >> 24;
return byte;
}

intValue(byte[] bytes) {
int myInt = bytes[3];
myInt = (myInt << | bytes[2];
myInt = (myInt << | bytes[1];
myInt = (myInt << | bytes[0];
return myInt;
}

 
Reply With Quote
 
 
 
 
Steve Horsley
Guest
Posts: n/a
 
      04-13-2005
carmen wrote:
> Hi,
>
> Could anyone suggest a way to convert a short to a 2 byte array and a
> int to a 4 byte array and vice versa? Is there any way to do it using
> utility classes provided by java?
> I have just begun to code in java ,
>
> regards,
> Carmen
>


The inbuilt utilities are probably to wrap a DataOutputStream
round a ByteArrayOutputStream, and DataInputStream round a
ByteArrayInputStream, but foo's bit-shifting ways are probably
quicker and easier.

Steve
 
Reply With Quote
 
erikstenqvist erikstenqvist is offline
Junior Member
Join Date: Jan 2009
Posts: 1
 
      01-13-2009
Hi,
I was looking for the answer to the same question (with shorts ) and got a hint from foo's answer, but didn't get it to work. So I decided to learn the basics about bits to solve it. I had a lot of help from the Campfire story "Cat and Mouse Games with Bits" at the site "JavaRanch".
Then I found my own solution of the problem, and then i found that it was almost identical with foo's.
I think that the reason why foo's code didn't work for me was (not only that 8 followed by ) looks like a ) that when you add the bytes bitwise java treats them like integers and fills remaining bytes with ones or zeros depending on sign. In foo's code it can probably be solved by adding the "hexBase" to the second part. Turning this:
Quote:
Originally Posted by foo
...
myInt = (myInt << 8 ) | bytes[2];
...
to something similar to this:
...
myInt = (myInt << 8 ) | (bytes[2] & hexBase ) ;
...
At least my code is working now.
Thank you all!
Erik
 
Reply With Quote
 
ersecchio ersecchio is offline
Junior Member
Join Date: Jan 2010
Posts: 1
 
      01-12-2010
I have an array of byte:
byte number = new byte[2];

all numerical valure int array "number" are expressend as multi-byte integer.
I would convert the numer into array "number" as int (integer).

how made?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
converting floating point number to integer and vice versa FPGA VHDL 5 01-08-2008 06:29 AM
convert ASCII to byte an vice versa chen li Ruby 3 07-13-2007 01:22 PM
Re: converting epoch time to string (and vice versa) Amit Khemka Python 1 03-13-2007 11:57 AM
converting epoch time to string (and vice versa) Astan Chee Python 1 03-13-2007 11:15 AM
Byte string to Unicode & vice versa Eugene Anthony ASP General 1 04-28-2006 09:57 AM



Advertisments