This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes int to byte[] Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "int to byte[]" Watch "int to byte[]" New topic
Author

int to byte[]

Steve Chaloner
Greenhorn

Joined: Jan 07, 2005
Posts: 22
Hi all,

not strictly a SCJD question but it's what it will be used in, so...

I want to convert an int to a byte array. I've tried using BigInteger to convert it to a binary string and from there break down the octets into bytes but that didn't work. Does anyone have another solution?

Thanks,
Steve


Steve Chaloner<br />-----------<br />SCJP 1.4<br />SCWCD 1.4
Andris Jekabsons
Ranch Hand

Joined: Jan 20, 2004
Posts: 82
How about this?From there this could be used/converted as needed.
Steve Chaloner
Greenhorn

Joined: Jan 07, 2005
Posts: 22
Converting the int to a binary string isn't a problem - my existing code is virtually to what you suggested:



The problem comes when I convert each octet to a byte:


Testing with the int value 245 (00000000000000000000000011110101), there are no problems for the first 3 iterations. However, for the last octet I receive the following exception:

Andris Jekabsons
Ranch Hand

Joined: Jan 20, 2004
Posts: 82
Well, the byte number range is -128 to +127, and 245 is out of that range.
Since I don't know what you are trying to achieve, then my best uneducated advice would be to change the logic of the program.
Andris
Frans Janssen
Ranch Hand

Joined: Dec 29, 2004
Posts: 357
Hi Steve,

Assuming you need this conversion to store integers into your database file, consider looking at the RandomAccessFile's method writeInt(). It does already what you need.

See RandomAccessFile javadoc.

Good luck,

Frans.


SCJP 1.4, SCJD
Steve Chaloner
Greenhorn

Joined: Jan 07, 2005
Posts: 22

Well, the byte number range is -128 to +127, and 245 is out of that range.


That's why the conversion results in a byte array, not a byte: 32-bit integer -> 4 8-bit bytes


Assuming you need this conversion to store integers into your database file, consider looking at the RandomAccessFile's method writeInt(). It does already what you need.


My HotelBookingRecord stores everything as byte arrays. The byte conversion was for a decorator that allowed you to interact with it using String (no problem there), booleans and integers.

I probably should rethink the strategy - but that's for later today

Thanks to you both for discussing this.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: int to byte[]
 
Similar Threads
?: operator
About numeric promotion
while loop
how to convert byte [] to String?
BYTE increment