int to byte array

Discussion in 'Java' started by toffe, Apr 6, 2004.

  1. toffe

    toffe Guest

    Hi all,

    I need an efficient way of turning an int into a byte array.

    Examples:

    int byte array
    1) 0 -> {0}
    2) 1 -> {1}
    3) 256 -> {1,0}
    4) 70000 -> {1,17,112} (since 70000 = 1 * 256^2 + 17 * 256 + 112)


    I can think of a few more or less odd ways to do this,
    but I need an efficient one. I suspect that there is some function
    already built in into Java that does this very thing.

    Any help on this one?

    Cheers,
    toffe
     
    toffe, Apr 6, 2004
    #1
    1. Advertising

  2. toffe wrote:
    >
    > I need an efficient way of turning an int into a byte array.
    >
    > Examples:
    >
    > int byte array
    > 1) 0 -> {0}
    > 2) 1 -> {1}
    > 3) 256 -> {1,0}
    > 4) 70000 -> {1,17,112} (since 70000 = 1 * 256^2 + 17 * 256 + 112)
    >
    >
    > I can think of a few more or less odd ways to do this,
    > but I need an efficient one. I suspect that there is some function
    > already built in into Java that does this very thing.
    >


    Strictly speaking, this isn't even possible because a "byte" in java is a signed
    entity - so values greater than 127 aren't allowed (whereas you actually need to
    store unsigned numbers between 0 and 255).

    In any case, the easiest (and probably most efficient way) of doing this is to
    right shift the int value by 0,8,16 and 24 bits and "and"ing with 0xFF.

    BK
     
    Babu Kalakrishnan, Apr 6, 2004
    #2
    1. Advertising

  3. toffe

    Chris Smith Guest

    Babu Kalakrishnan wrote:
    > Strictly speaking, this isn't even possible because a "byte" in java is a signed
    > entity - so values greater than 127 aren't allowed (whereas you actually need to
    > store unsigned numbers between 0 and 255).


    I've sometimes found it easier to deal with data under the model that
    the signed-ness of a value depends on the operation rather than the
    value's type. Operations can then be grouped into three categories:
    signed, unsigned, and "doesn't matter". For the most part, it's then
    possible to deal with signed 8-bit byte values by avoiding the class of
    "unsigned" operations, and vice versa.

    Since all standard API string conversion routines fall into the
    "signed" category, bytes definitely look signed, but don't have to be
    treated that way. There are, however, a few "signed" operations that
    have no unsigned equivalent. In those cases, it's necessary to convert
    to a larger data type for the temporary result.

    This model is less consistent with the vocabulary of most reference
    sources and the JLS, but is equally effective at explaining the behavior
    of the language.

    --
    www.designacourse.com
    The Easiest Way to Train Anyone... Anywhere.

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
     
    Chris Smith, Apr 6, 2004
    #3
  4. toffe

    toffe Guest

    Babu Kalakrishnan wrote:
    > toffe wrote:
    >
    >>
    >> I need an efficient way of turning an int into a byte array.

    > right shift the int value by 0,8,16 and 24 bits and "and"ing with 0xFF.
    >
    > BK
    >


    Thank you very much. It worked (I can pretend the bytes are unsigned).

    -toffe
     
    toffe, Apr 8, 2004
    #4
  5. toffe

    Dave Monroe Guest

    toffe <> wrote in message news:<vVwcc.88868$>...
    > Hi all,
    >
    > I need an efficient way of turning an int into a byte array.
    >
    > Examples:
    >
    > int byte array
    > 1) 0 -> {0}
    > 2) 1 -> {1}
    > 3) 256 -> {1,0}
    > 4) 70000 -> {1,17,112} (since 70000 = 1 * 256^2 + 17 * 256 + 112)
    >
    >
    > I can think of a few more or less odd ways to do this,
    > but I need an efficient one. I suspect that there is some function
    > already built in into Java that does this very thing.
    >
    > Any help on this one?
    >
    > Cheers,
    > toffe



    How about:
    int x = 7000;
    String s = x + "";
    byte [] barr = s.getBytes();

    Dave Monroe
     
    Dave Monroe, Apr 9, 2004
    #5
    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. Kirby
    Replies:
    3
    Views:
    682
    Kirby
    Oct 8, 2004
  2. Schnoffos
    Replies:
    2
    Views:
    1,254
    Martien Verbruggen
    Jun 27, 2003
  3. Hal Styli
    Replies:
    14
    Views:
    1,716
    Old Wolf
    Jan 20, 2004
  4. Lew

    Re: byte + byte -> int

    Lew, Mar 20, 2009, in forum: Java
    Replies:
    6
    Views:
    401
    Arne Vajhøj
    Mar 21, 2009
  5. Alain Spineux
    Replies:
    6
    Views:
    657
    Eric Sosman
    May 17, 2011
Loading...

Share This Page