How to convert byte[] into a SINGLE integer ?

Discussion in 'Java' started by Spendius, Sep 4, 2003.

  1. Spendius

    Spendius Guest

    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
     
    Spendius, Sep 4, 2003
    #1
    1. Advertising

  2. 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] << 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:
    PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)

    Always remember: The answer is forty-two, there can only be wrong
    questions!
     
    Lothar Kimmeringer, Sep 4, 2003
    #2
    1. Advertising

  3. On 4 Sep 2003 08:13:19 -0700, (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/
     
    Marco Parmeggiani, Sep 4, 2003
    #3
  4. Spendius

    Brad BARCLAY Guest

    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/...?rev=1.7&content-type=text/vnd.viewcvs-markup

    HTH!

    Brad BARCLAY

    --
    =-=-=-=-=-=-=-=-=
    From the OS/2 WARP v4.5 Desktop of Brad BARCLAY.
    The jSyncManager Project: http://www.jsyncmanager.org
    
     
    Brad BARCLAY, Sep 4, 2003
    #4
  5. Spendius

    Roedy Green Guest

    Roedy Green, Sep 4, 2003
    #5
  6. Spendius

    Spendius Guest

    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
     
    Spendius, Sep 5, 2003
    #6
  7. On 5 Sep 2003 04:15:55 -0700, (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/
     
    Marco Parmeggiani, Sep 5, 2003
    #7
  8. Spendius

    Brad BARCLAY Guest

    Roedy Green wrote:
    > On Thu, 04 Sep 2003 17:37:59 GMT, Brad BARCLAY
    > <> wrote or quoted :
    >
    >
    >>http://cvs.sourceforge.net/cgi-bin/...?rev=1.7&content-type=text/vnd.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
     
    Brad BARCLAY, Sep 5, 2003
    #8
  9. Spendius

    Brad BARCLAY Guest

    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
    
     
    Brad BARCLAY, Sep 5, 2003
    #9
  10. 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 & 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;
    }
    --
    Daniel Sjöblom
     
    =?ISO-8859-1?Q?Daniel_Sj=F6blom?=, Sep 5, 2003
    #10
  11. Daniel Sjöblom wrote:

    > 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;
    }
    --
    Daniel Sjöblom
     
    =?ISO-8859-1?Q?Daniel_Sj=F6blom?=, Sep 5, 2003
    #11
  12. Spendius

    Spendius Guest

    Marco Parmeggiani <> wrote in message ...
    > 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.
     
    Spendius, Sep 5, 2003
    #12
  13. Spendius

    Roedy Green Guest

    On 5 Sep 2003 04:15:55 -0700, (Spendius) wrote
    or quoted :

    >4 hex values 00 00 00 0a, I'd be returned the value 10...


    You might look at the code at http://mindprod.com/jgloss/endian.html
    that packs and unpacks various primitives out of bytes in big/little
    endian.

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
     
    Roedy Green, Sep 5, 2003
    #13
  14. Spendius

    Roedy Green Guest

    On Fri, 05 Sep 2003 20:53:05 +0300, Daniel Sjöblom
    <_NOSPAM> wrote or quoted :

    >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.


    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
     
    Roedy Green, Sep 5, 2003
    #14
  15. Roedy Green wrote:
    > On Fri, 05 Sep 2003 20:53:05 +0300, Daniel Sjöblom
    > <_NOSPAM> wrote or quoted :
    >
    >
    >>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);
    }
    --
    Daniel Sjöblom
     
    =?ISO-8859-1?Q?Daniel_Sj=F6blom?=, Sep 6, 2003
    #15
  16. Spendius

    Roedy Green Guest

    On Sat, 06 Sep 2003 21:41:41 +0300, Daniel Sjöblom
    <_NOSPAM> wrote or quoted :

    >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;
    }

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
     
    Roedy Green, Sep 6, 2003
    #16
  17. Spendius

    Jon Skeet Guest

    Brad BARCLAY <> wrote:
    > 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.

    --
    Jon Skeet - <>
    http://www.pobox.com/~skeet/
    If replying to the group, please do not mail me too
     
    Jon Skeet, Sep 8, 2003
    #17
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Polaris431
    Replies:
    8
    Views:
    761
    SM Ryan
    Dec 4, 2006
  2. Replies:
    1
    Views:
    483
    Thomas J. Gritzan
    Jul 20, 2006
  3. Replies:
    4
    Views:
    1,372
  4. Godzilla
    Replies:
    15
    Views:
    511
    jigloo
    Jul 15, 2007
  5. Maligree

    convert n-byte string into integer

    Maligree, Apr 28, 2010, in forum: Python
    Replies:
    2
    Views:
    422
    Maligree
    Apr 28, 2010
Loading...

Share This Page