String str = Integer.toBinaryString(123
byte[] byte_array = str.getBytes();
Main Topics
Browse All TopicsI 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
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Hi,
Try this:
int value = 0xcafebabe;
byte[] bytes = new byte[4];
for( int i=0; i<bytes.length; ++i ){
int offset = (bytes.length-i-1)*8;
bytes[i] = (value & (0xff << offset)) >>> offset;
}
for( int i=0; i<bytes.length; ++i ){
System.out.print(Integer.t
}
System.out.println();
Regards,
Igor Bazarny,
Brainbench MVP for Java 1
www.brainbench.com
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;
}
Business Accounts
Answer for Membership
by: Venci75Posted on 2002-03-04 at 07:11:48ID: 6839117
try this:
"101".getBytes();