gordon_guan
asked on
convert int(short or long) to byte array
I intend to convert a integer (or short or long) that is greater than a byte can hold (eg, 1111) to a byte array, since casting is simply cannot be used here, any other way can do it without lose precise? I have tried to use Integer wrapper class and use its toBinaryString() to convert the int to a string representation, eg 5->"101", then I really cannot find a way of converting "101" to a byte array with correct binary representation. Please help
String str = Integer.toBinaryString(123 45);
byte[] byte_array = str.getBytes();
byte[] byte_array = str.getBytes();
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
hello vinci, I would say your answer is not the thing that I want and in my case, it is wrong. I would rather prefer bazany's answer since what he/she does is more or less the same as what I did. So please change the answer back. Thanks.
ASKER
hello vinci, I would say your answer is not the thing that I want and in my case, it is wrong. I would rather prefer bazany's answer since what he/she does is more or less the same as what I did. So please change the answer back. Thanks.
Here's another way:
private byte[] getBytes(int n) {
BigInteger big = new BigInteger(Integer.toStrin g(n));
return big.toByteArray();
}
private byte[] getBytes(int n) {
BigInteger big = new BigInteger(Integer.toStrin
return big.toByteArray();
}
Assuming v is the value of the integer you are trying to convert to bytes, try this:
byte b[] = new byte[4];
int i, shift;
for(i = 0, shift = 24; i < 4; i++, shift -= 8)
b[i] = (byte)(0xFF & (v >> shift));
byte b[] = new byte[4];
int i, shift;
for(i = 0, shift = 24; i < 4; i++, shift -= 8)
b[i] = (byte)(0xFF & (v >> shift));
This is a more readable (and faster) solution than unidyne's.
I've also included the code to recreate the int from the byte array. This is a fragment of a class I wrote called ByteArrayUtils; if anyone can give me a faster solution, I'd be very grateful!
Enjoy!
public static final int NUMBER_OF_BITS_IN_A_BYTE = 8;
public static final short MASK_TO_BYTE = 0xFF;
public static final int SIZE_OF_AN_INT_IN_BYTES = 4;
public static void writeInt( byte[] p_dest, // not allocated and returned so array can be reused
int p_toWrite )
{
assert( p_dest.length >= SIZE_OF_AN_INT_IN_BYTES )
: "Programming error: p_dest is too short to hold an int";
// unrolled loop of 4 iterations
p_dest[0] = p_toWrite & MASK_TO_BYTE );
p_toWrite >>= NUMBER_OF_BITS_IN_A_BYTE;
p_dest[1] = p_toWrite & MASK_TO_BYTE );
p_toWrite >>= NUMBER_OF_BITS_IN_A_BYTE;
p_dest[2] = p_toWrite & MASK_TO_BYTE );
p_toWrite >>= NUMBER_OF_BITS_IN_A_BYTE;
p_dest[3] = p_toWrite & MASK_TO_BYTE );
}
public static int readInt( byte[] p_src ) // must be of size 4
{
assert( p_src.length >= SIZE_OF_AN_INT_IN_BYTES )
: "Programming error: p_src is too short to hold an int";
// unrolled loop of 4 iterations
int result = ( p_src[ 0 ] & MASK_TO_BYTE );
result |= ( ( p_src[ 1 ] & MASK_TO_BYTE ) << 8 );
result |= ( ( p_src[ 2 ] & MASK_TO_BYTE ) << 16 );
result |= ( ( p_src[ 3 ] & MASK_TO_BYTE ) << 24 );
return result;
}
I've also included the code to recreate the int from the byte array. This is a fragment of a class I wrote called ByteArrayUtils; if anyone can give me a faster solution, I'd be very grateful!
Enjoy!
public static final int NUMBER_OF_BITS_IN_A_BYTE = 8;
public static final short MASK_TO_BYTE = 0xFF;
public static final int SIZE_OF_AN_INT_IN_BYTES = 4;
public static void writeInt( byte[] p_dest, // not allocated and returned so array can be reused
int p_toWrite )
{
assert( p_dest.length >= SIZE_OF_AN_INT_IN_BYTES )
: "Programming error: p_dest is too short to hold an int";
// unrolled loop of 4 iterations
p_dest[0] = p_toWrite & MASK_TO_BYTE );
p_toWrite >>= NUMBER_OF_BITS_IN_A_BYTE;
p_dest[1] = p_toWrite & MASK_TO_BYTE );
p_toWrite >>= NUMBER_OF_BITS_IN_A_BYTE;
p_dest[2] = p_toWrite & MASK_TO_BYTE );
p_toWrite >>= NUMBER_OF_BITS_IN_A_BYTE;
p_dest[3] = p_toWrite & MASK_TO_BYTE );
}
public static int readInt( byte[] p_src ) // must be of size 4
{
assert( p_src.length >= SIZE_OF_AN_INT_IN_BYTES )
: "Programming error: p_src is too short to hold an int";
// unrolled loop of 4 iterations
int result = ( p_src[ 0 ] & MASK_TO_BYTE );
result |= ( ( p_src[ 1 ] & MASK_TO_BYTE ) << 8 );
result |= ( ( p_src[ 2 ] & MASK_TO_BYTE ) << 16 );
result |= ( ( p_src[ 3 ] & MASK_TO_BYTE ) << 24 );
return result;
}
"101".getBytes();