Convert a long into 8 bytes

Hi everybody

An easy question. I have a long (64 bits) and I want to convert it into 8 bytes (8 * 8 bits). Is there any easy way in order to do this? How can I do it?

I have tried converting the long into a String of 64 bits with the "Long.toBinaryString" function and then using "Byte.pardeByte" to obtain the bytes. However, I get an error when trying to convert the String "11111111" into a byte. Any ideas?

Thanks in advance!

[454 byte] By [astu18a] at [2007-11-27 11:49:33]
# 1

Byte.parseByte doesn't parse strings with binary representations to a byte.

byte[] b = new byte[8];

b[0] = (v >> 56) & 0xFF;

b[1] = (v >> 48) & 0xFF;

b[2] = (v >> 40) & 0xFF;

b[3] = (v >> 32) & 0xFF;

b[4] = (v >> 24) & 0xFF;

b[5] = (v >> 16) & 0xFF;

b[6] = (v >> 8) & 0xFF;

b[7] = v & 0xFF;

bsampieria at 2007-7-29 18:25:58 > top of Java-index,Java Essentials,Java Programming...
# 2

Thanks a lot!! I am going to test it. Thank you!

astu18a at 2007-7-29 18:25:58 > top of Java-index,Java Essentials,Java Programming...