How to convert byte[] into a SINGLE integer ?

S

Spendius

Hi,
I searched, but to no avail: I just want to transform
for ex. the array:
byte[] b = new byte[2];
for (k=0; k<2; k++) b[k] = rMap.get();
int j = toInt(b);
System.out.println("j is equal to "+j);

('rMap.get()' being the retrieving of values from a
binary file) into an integer value (say if -in hex. notation-
I have b[0] == ab and b[1] == 8f the call to a function
toInt(byte[] b) would return 43919:

# j is equal to 43919

Thanks...
Spendius
 
L

Lothar Kimmeringer

byte[] b = new byte[2];
for (k=0; k<2; k++) b[k] = rMap.get();
int j = toInt(b);
System.out.println("j is equal to "+j);

('rMap.get()' being the retrieving of values from a
binary file) into an integer value (say if -in hex. notation-
I have b[0] == ab and b[1] == 8f the call to a function
toInt(byte[] b) would return 43919:

int contains four bytes, two-bytes-values is represented
by short. What you need is bit-shifting being done with
<< or >>. So when the higher byte is at the first element
of the byte-array your toInt would look like this:

public int toInt(byte[] b){
return (b[0] << 8) |
b[1];
}

If you want to do it with "real" int-values, you
need to shift the two other byte-values 16 bits
and 24 bits.


Regards, Lothar
--
Lothar Kimmeringer E-Mail: (e-mail address removed)
PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)

Always remember: The answer is forty-two, there can only be wrong
questions!
 
M

Marco Parmeggiani

Hi,
I searched, but to no avail: I just want to transform
for ex. the array:
byte[] b = new byte[2];
for (k=0; k<2; k++) b[k] = rMap.get();
int j = toInt(b);
System.out.println("j is equal to "+j);

('rMap.get()' being the retrieving of values from a
binary file) into an integer value (say if -in hex. notation-
I have b[0] == ab and b[1] == 8f the call to a function
toInt(byte[] b) would return 43919:

# j is equal to 43919

Thanks...
Spendius

so, MSB is in position 0:

int i;
i=0;
for (k=0; k<2; k++) {
i += b[k] << (8*(2-k-1));
}

ciao
 
B

Brad BARCLAY

Spendius said:
Hi,
I searched, but to no avail: I just want to transform
for ex. the array:

byte[] b = new byte[2];
for (k=0; k<2; k++) b[k] = rMap.get();
int j = toInt(b);
System.out.println("j is equal to "+j);

I wrote a utility class for my Open Source project to handle these
sorts of conversions. It's currently using mathematical operations to
do the conversions, but I'm going to get around to changing it to using
bit-manipulations one of these days. you can view the source here:

http://cvs.sourceforge.net/cgi-bin/...?rev=1.7&content-type=text/vnd.viewcvs-markup

HTH!

Brad BARCLAY
 
S

Spendius

Er... Sorry but none of your solutions work. If I edit my
bin. file with a hex editor for instance, where I see the
line
# 07 03 23 23 23 04 63 63 20 20 3c 01 03 02 c1 06
^^ ^^ ^^ ^^
if I put the 4 bytes 63, 20, 20 and 3c in a four-position
array, I'd like my 'toInt(byte[] b)' function to return
me the value 1 663 049 788. To be simpler if I had the
4 hex values 00 00 00 0a, I'd be returned the value 10...

With your stuffs when I have an hex value of 50, I'm returned
an integer value of 50 ! Yet 50 in hex == 80 in decimal
notation...

Once again thanks...
Spendius
 
M

Marco Parmeggiani

With your stuffs when I have an hex value of 50, I'm returned
an integer value of 50 ! Yet 50 in hex == 80 in decimal
notation...

well, i think you should elaborate a bit the code that we provided to
you. It's obvious that some of the code posted works only with arrays of
two bytes. You should take it as an example i guess.

ciao
 
B

Brad BARCLAY

Roedy said:

I know about it -- I'm just far too lazy to actually use it. Besides
which, if in a year or so their service disappears for one reason or
another, I know that if someone does a Goolge groups search (or the
like) and finds one of my responses, the definitive URL will probably
still be around (either that or the resource is gone for good, in which
case neither the real nor the Tiny URL is going to help anybody).

But, for the record, I have recently shortened access to the ViewCVS
tree for my project by setting up a stealth redirect through
http://viewcvs.jsyncmanager.org.

Brad BARCLAY
 
B

Brad BARCLAY

Spendius said:
Er... Sorry but none of your solutions work. If I edit my
bin. file with a hex editor for instance, where I see the
line
# 07 03 23 23 23 04 63 63 20 20 3c 01 03 02 c1 06
^^ ^^ ^^ ^^
if I put the 4 bytes 63, 20, 20 and 3c in a four-position
array, I'd like my 'toInt(byte[] b)' function to return
me the value 1 663 049 788. To be simpler if I had the
4 hex values 00 00 00 0a, I'd be returned the value 10...

With your stuffs when I have an hex value of 50, I'm returned
an integer value of 50 ! Yet 50 in hex == 80 in decimal
notation...

Real bytes have no real format -- they're just a value. It's up to the
language to provide representations of the bytes, and typically this
will be in decimal, unless you set the representation accordingly (for
hard-coded values, '50' in Java will be decimal 50, wheras for hex
you'll want to use "0x50").

If you're doing the conversions from Strings, then you're not really
working with byte values, but with string values that represent a byte.

In the future it is a good idea to quote at least a small piece of the
post you're replying to so people know which post in the thread you're
commenting on.

Brad BARCLAY
 
?

=?ISO-8859-1?Q?Daniel_Sj=F6blom?=

Spendius said:
Er... Sorry but none of your solutions work. If I edit my
bin. file with a hex editor for instance, where I see the
line
# 07 03 23 23 23 04 63 63 20 20 3c 01 03 02 c1 06
^^ ^^ ^^ ^^
if I put the 4 bytes 63, 20, 20 and 3c in a four-position
array, I'd like my 'toInt(byte[] b)' function to return
me the value 1 663 049 788. To be simpler if I had the
4 hex values 00 00 00 0a, I'd be returned the value 10...

With your stuffs when I have an hex value of 50, I'm returned
an integer value of 50 ! Yet 50 in hex == 80 in decimal
notation...

Once again thanks...
Spendius

This is a pretty suboptimal solution, but it works as you said (except I
use long instead of int), and it works for quite long hex arrays . If
you don't need long arrays, I included another version which is faster.

static long byteToLong(byte[] b)
{
long val = 0;
for (int i = b.length-1, j = 0; i >= 0; i--,j += 2 )
{
// low 4 bits
int tmp = b & 0x0f;
// high 4 bits
int tmp2 = (b & 0xf0) >> 4;
// multiply by base
val += tmp * (Math.pow(16, j));
val += tmp2 * (Math.pow(16, j+1));
}
return val;
}

static int byteToInt(byte[] b)
{
int val=0;
for (int i=b.length-1, j = 0; i >= 0; i--,j++)
{
val += (b & 0xff) << (8*j);
}
return val;
}
 
?

=?ISO-8859-1?Q?Daniel_Sj=F6blom?=

Daniel said:
This is a pretty suboptimal solution, but it works as you said (except I
use long instead of int), and it works for quite long hex arrays . If
you don't need long arrays, I included another version which is faster.

static long byteToLong(byte[] b)
{
long val = 0;
for (int i = b.length-1, j = 0; i >= 0; i--,j += 2 )
{
// low 4 bits
int tmp = b & 0x0f;
// high 4 bits
int tmp2 = (b & 0xf0) >> 4;
// multiply by base
val += tmp * (Math.pow(16, j));
val += tmp2 * (Math.pow(16, j+1));
}
return val;
}


Better version:

static long byteToLong(byte[] b)
{
long val = 0;
for (int i = b.length - 1, j = 0; i >= 0; i--, j += 2)
{
val += (b & 0xff) * Math.pow(16,j);
}
return val;
}
 
S

Spendius

Marco Parmeggiani said:
well, i think you should elaborate a bit the code that we
provided to
you. It's obvious that some of the code posted works only
with arrays of
two bytes. You should take it as an example i guess.
No, because in the loop you gave me:
#int i;
#i=0;
#for (int k=0; k<2; k++) {
# i += b[k] << (8*(2-k-1));
#}
I replaced 2 with "b.length" to deal with arrays of any (and
unknown length), and I still get wrong results. I also found
things like
# ..
# i += (b[k] & 0xff) << (8 * (l-k-1));
and so on but it returns me the same erroneous values.

I couldn't manage to find a solution myself.
Thanks.
 
R

Roedy Green

val += (b & 0xff) * Math.pow(16,j);
}


Please no. Math.pow is a very expensive operation. You can get the
same effect with a shift much more cheaply. Further is implies
conversion to double.
 
?

=?ISO-8859-1?Q?Daniel_Sj=F6blom?=

Roedy said:
val += (b & 0xff) * Math.pow(16,j);
}



Please no. Math.pow is a very expensive operation. You can get the
same effect with a shift much more cheaply. Further is implies
conversion to double.


Yes, I realize that now. But you still need a cast to long. So change
that to:

for (int i = b.length - 1, j = 0; i >= 0; i--, j++)
{
val += (long)(b & 0xff) << (8*j);
}
 
R

Roedy Green

for (int i = b.length - 1, j = 0; i >= 0; i--, j++)
{
val += (long)(b & 0xff) << (8*j);
}
--



You could speed that up a little with:

for (int i = b.length - 1, j = 0; i >= 0; i--, j+=8)
{
val |= ((long)(b & 0xff)) << j;
}
 
J

Jon Skeet

Brad BARCLAY said:
I know about it -- I'm just far too lazy to actually use it. Besides
which, if in a year or so their service disappears for one reason or
another, I know that if someone does a Goolge groups search (or the
like) and finds one of my responses, the definitive URL will probably
still be around (either that or the resource is gone for good, in which
case neither the real nor the Tiny URL is going to help anybody).

In that case the best thing to do is to quote both the TinyURL version
and the original.

Personally I wish TinyURL would make their database available in some
form, so that:

a) If TinyURL *does* go down the pan, the URLs needn't be useless,
because someone else could resurrect tinyurl.com

b) Other sites could give a preview of where the user is going to be
redirected to *without* actually redirecting - some people don't trust
tinyurl URLs as they could go to anywhere in the end, including porn
etc.
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top