byte to long: convertion problem

C

cccc

Hi,

I want translate 8 byte to a long.
But I am a problem.

The source code that I am using is:

char[] data={0x3f, 0xe7, 0x5d, 0x40, 0x5f, 0x50, 0xcc, 0x33};

long as =
(0xFF & data[0]) << 56 |
(0xFF & data[1]) << 48 |
(0xFF & data[2]) << 40 |
(0xFF & data[3]) << 32 |
(0xFF & data[4]) << 24 |
(0xFF & data[5]) << 16 |
(0xFF & data[6]) << 8 |
(0xFF & data[7]);

System.out.println("BIN="+Long.toBinaryString(as));
System.out.println("HEX="+Long.toHexString(as));

Why first print show 32 bit instead of 64?
Why second print show a hexadecimal value different to array?

You can help me?

Ciao,
CC
 
J

Jonck

In said:
Hi,

I want translate 8 byte to a long.

Hi,
Check out Roedy Green's Conversion Amanuensis, that'll tell you all you
will ever need to know on conversion issues.

Cheers, Jonck
 
W

Wolf Martinus

cccc said:
long as =
(0xFF & data[0]) << 56 |

you need to cast to long before shifting or the shifting op will be
limited to int range (32Byte):

long as = (long) data[0] <<56 [...];
 
T

Thomas G. Marshall

andreas coughed up:
this is, of course, not an easy basis to start with... :)

andreas

:) But the usenet is fully global. English is a 2nd (or 3rd) language for
many of its perusers.

I try to automatically slack cut ( anguished english idiom :) ) in this
regard.
 
J

John Davison

cccc said:
Hi,

I want translate 8 byte to a long.
But I am a problem.

The source code that I am using is:

char[] data={0x3f, 0xe7, 0x5d, 0x40, 0x5f, 0x50, 0xcc, 0x33};

long as =
(0xFF & data[0]) << 56 |
(0xFF & data[1]) << 48 |
(0xFF & data[2]) << 40 |
(0xFF & data[3]) << 32 |
(0xFF & data[4]) << 24 |
(0xFF & data[5]) << 16 |
(0xFF & data[6]) << 8 |
(0xFF & data[7]);

System.out.println("BIN="+Long.toBinaryString(as));
System.out.println("HEX="+Long.toHexString(as));

Why first print show 32 bit instead of 64?
Why second print show a hexadecimal value different to array?

You can help me?

Ciao,
CC

Get the source code to the Java Standard Library and look at
java/io/Bits.java

This is what the classes use to convert primary types into streams of
bytes, and vice versa.

- john
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,479
Members
44,900
Latest member
Nell636132

Latest Threads

Top