Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > How to convert byte[] into a SINGLE integer ?

Reply
Thread Tools

How to convert byte[] into a SINGLE integer ?

 
 
Spendius
Guest
Posts: n/a
 
      09-04-2003
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
 
Reply With Quote
 
 
 
 
Lothar Kimmeringer
Guest
Posts: n/a
 
      09-04-2003
On 4 Sep 2003 08:13:19 -0700, Spendius wrote:

>> 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] << |
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: http://www.velocityreviews.com/forums/(E-Mail Removed)
PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)

Always remember: The answer is forty-two, there can only be wrong
questions!
 
Reply With Quote
 
 
 
 
Marco Parmeggiani
Guest
Posts: n/a
 
      09-04-2003
On 4 Sep 2003 08:13:19 -0700, (E-Mail Removed) (Spendius) wrote:

>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
--
Seti@Home Java Applets
http://maruko.webhop.net/
 
Reply With Quote
 
Brad BARCLAY
Guest
Posts: n/a
 
      09-04-2003
Spendius wrote:
> 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/v...viewcvs-markup

HTH!

Brad BARCLAY

--
=-=-=-=-=-=-=-=-=
From the OS/2 WARP v4.5 Desktop of Brad BARCLAY.
The jSyncManager Project: http://www.jsyncmanager.org


 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      09-04-2003
On Thu, 04 Sep 2003 17:37:59 GMT, Brad BARCLAY
<(E-Mail Removed)> wrote or quoted :

>http://cvs.sourceforge.net/cgi-bin/v...viewcvs-markup


see http://mindprod.com/jgloss/tinyurl.html

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
 
Reply With Quote
 
Spendius
Guest
Posts: n/a
 
      09-05-2003
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
 
Reply With Quote
 
Marco Parmeggiani
Guest
Posts: n/a
 
      09-05-2003
On 5 Sep 2003 04:15:55 -0700, (E-Mail Removed) (Spendius) wrote:


>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
--
Seti@Home Java Applets
http://maruko.webhop.net/
 
Reply With Quote
 
Brad BARCLAY
Guest
Posts: n/a
 
      09-05-2003
Roedy Green wrote:
> On Thu, 04 Sep 2003 17:37:59 GMT, Brad BARCLAY
> <(E-Mail Removed)> wrote or quoted :
>
>
>>http://cvs.sourceforge.net/cgi-bin/v...viewcvs-markup

>
>
> see http://mindprod.com/jgloss/tinyurl.html


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

--
=-=-=-=-=-=-=-=-=
From the OS/2 WARP v4.5 Desktop of Brad BARCLAY.
The jSyncManager Project: http://www.jsyncmanager.org

 
Reply With Quote
 
Brad BARCLAY
Guest
Posts: n/a
 
      09-05-2003
Spendius wrote:
> 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

--
=-=-=-=-=-=-=-=-=
From the OS/2 WARP v4.5 Desktop of Brad BARCLAY.
The jSyncManager Project: http://www.jsyncmanager.org


 
Reply With Quote
 
=?ISO-8859-1?Q?Daniel_Sj=F6blom?=
Guest
Posts: n/a
 
      09-05-2003
Spendius wrote:
> 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[i] & 0x0f;
// high 4 bits
int tmp2 = (b[i] & 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[i] & 0xff) << (8*j);
}
return val;
}
--
Daniel Sj÷blom







 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
resolve single line with multiple items into mutliple lines, single items ela Perl Misc 12 04-06-2009 06:47 PM
Convert IEEE single from integer representation A. Sinan Unur Perl Misc 5 03-11-2007 07:26 PM
how to convert "BigInteger" into "Integer"? how to print out a BigInteger binary value? nick Java 0 10-26-2004 08:18 AM
convert scientific integer to normal integer les ander Python 4 10-05-2004 04:26 PM
Convert numeric String into Integer (Java) Azmie Java 12 12-30-2003 04:33 AM



Advertisments