File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Convert from integer to array of bytes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Convert from integer to array of bytes" Watch "Convert from integer to array of bytes" New topic
Author

Convert from integer to array of bytes

Rob Chung
Ranch Hand

Joined: Oct 15, 2002
Posts: 46
Hi,
I am trying to convert any integer into 4 bytes. The following code continues to give me ffffff80 in byte 3. Please help. Thanks in advance!
byte [] aa = { 0, 0, 0, 0 };
int bb = 128;
aa[0] = (byte) ((bb >>> 24) & 0xFF);
aa[1] = (byte) ((bb >>> 16) & 0xFF);
aa[2] = (byte) ((bb >>> 8) & 0xFF);
aa[3] = (byte) ((bb >>> 0) & 0xFF);
Rob Chung
Ranch Hand

Joined: Oct 15, 2002
Posts: 46
This actually works.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Convert from integer to array of bytes
 
Similar Threads
arrays equals() confusion
MD5
unsigned integer
how to create an image through bufferedimage,image and other helper class using byte array
FileInputStream & Negative Bytes...