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?
Steve Chaloner<br />-----------<br />SCJP 1.4<br />SCWCD 1.4
How about this?From there this could be used/converted as needed.
Joined: Jan 07, 2005
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:
Joined: Jan 20, 2004
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