Convert int to char array (easy but...)

Discussion in 'Java' started by clinisbut, Nov 30, 2007.

  1. clinisbut

    clinisbut Guest

    What I want to do exactly is this:

    I need to split (in a binary way) an Int into two bytes.
    Something like:

    int value : 340
    same value in binary: 101010100

    convert this to 2 bytes:
    byte1 = 2 ( 1 in binary );
    byte2 = 84 ( 1010100 );

    I'm trying to do this but does not work:
    byte1 = value>>8;
    byte2 = value & 0x00FF;

    But gives me wrong values... What I'm doing wrong!?
     
    clinisbut, Nov 30, 2007
    #1
    1. Advertising

  2. clinisbut wrote:
    > What I want to do exactly is this:
    >
    > I need to split (in a binary way) an Int into two bytes.
    > Something like:
    >
    > int value : 340
    > same value in binary: 101010100
    >
    > convert this to 2 bytes:
    > byte1 = 2 ( 1 in binary );

    byte1 = 1 ( 1 in binary );

    > byte2 = 84 ( 1010100 );
    >
    > I'm trying to do this but does not work:
    > byte1 = value>>8;
    > byte2 = value & 0x00FF;
    >
    > But gives me wrong values... What I'm doing wrong!?

    You calculated wrong byte1. See above.
    I get byte1=1 and byte2=84.

    --
    Thomas
     
    Thomas Fritsch, Nov 30, 2007
    #2
    1. Advertising

  3. clinisbut

    Chris Dollin Guest

    clinisbut wrote:

    > What I want to do exactly is this:
    >
    > I need to split (in a binary way) an Int into two bytes.
    > Something like:
    >
    > int value : 340
    > same value in binary: 101010100
    >
    > convert this to 2 bytes:
    > byte1 = 2 ( 1 in binary );


    2 != 1.

    1 in binary is 1.

    --
    Chris "one is one and all alone and evermore shall be so" Dollin

    Hewlett-Packard Limited registered no:
    registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England
     
    Chris Dollin, Nov 30, 2007
    #3
  4. clinisbut

    Philipp Guest

    Thomas Fritsch wrote:
    > clinisbut wrote:
    >> What I want to do exactly is this:
    >>
    >> I need to split (in a binary way) an Int into two bytes.
    >> Something like:
    >>
    >> int value : 340
    >> same value in binary: 101010100
    >>
    >> convert this to 2 bytes:
    >> byte1 = 2 ( 1 in binary );

    > byte1 = 1 ( 1 in binary );
    >
    >> byte2 = 84 ( 1010100 );
    >>
    >> I'm trying to do this but does not work:
    >> byte1 = value>>8;
    >> byte2 = value & 0x00FF;
    >>
    >> But gives me wrong values... What I'm doing wrong!?

    > You calculated wrong byte1. See above.
    > I get byte1=1 and byte2=84.
    >


    You will also have to cast to byte...

    int value = 340;
    byte b1 = (byte)(value >> 8);
    byte b2 = (byte)(value & 0xFF);
     
    Philipp, Nov 30, 2007
    #4
  5. clinisbut

    Stefan Ram Guest

    clinisbut <> writes:
    >int value : 340
    >convert this to 2 bytes:
    >byte1 = 2 ( 1 in binary );
    >byte2 = 84 ( 1010100 );


    public class Main
    { public static void main( final java.lang.String[] args )
    { final java.nio.ByteBuffer buffer = java.nio.ByteBuffer.allocate( 4 );
    buffer.putInt( 340 ).position( 0 );
    for( int i = 0; i < 4; ++i )java.lang.System.out.printf
    ( "%8s%n", java.lang.Integer.toBinaryString( buffer.get() )); }}

    0
    0
    1
    1010100
     
    Stefan Ram, Nov 30, 2007
    #5
  6. clinisbut

    clinisbut Guest

    On 30 nov, 14:47, Thomas Fritsch <> wrote:
    > clinisbut wrote:
    > > What I want to do exactly is this:

    >
    > > I need to split (in a binary way) an Int into two bytes.
    > > Something like:

    >
    > > int value : 340
    > > same value in binary: 101010100

    >
    > > convert this to 2 bytes:
    > > byte1 = 2 ( 1 in binary );

    >
    > byte1 = 1 ( 1 in binary );
    >
    > > byte2 = 84 ( 1010100 );

    >
    > > I'm trying to do this but does not work:
    > > byte1 = value>>8;
    > > byte2 = value & 0x00FF;

    >
    > > But gives me wrong values... What I'm doing wrong!?

    >
    > You calculated wrong byte1. See above.
    > I get byte1=1 and byte2=84.
    >
    > --
    > Thomas


    Sorry, obviously I misstyped 1...
     
    clinisbut, Nov 30, 2007
    #6
  7. clinisbut

    Curt Welch Guest

    clinisbut <> wrote:
    > What I want to do exactly is this:
    >
    > I need to split (in a binary way) an Int into two bytes.
    > Something like:
    >
    > int value : 340
    > same value in binary: 101010100
    >
    > convert this to 2 bytes:
    > byte1 = 2 ( 1 in binary );
    > byte2 = 84 ( 1010100 );
    >
    > I'm trying to do this but does not work:
    > byte1 = value>>8;
    > byte2 = value & 0x00FF;
    >
    > But gives me wrong values... What I'm doing wrong!?


    Another thing to watch out for in this type of code...

    The >> operator will do a sign extend so if the high bit of the value is
    set (aka it's a negative number), it will fill the high bits with 1's
    instead of 0's. So depending on the variable sizes you are working with,
    you sometimes have to AND out the high bits after a >> like this:

    short value = (short) 0xFFFF; // 16 ones (aka same as -1 for short);
    short byte1;

    byte1 = (short)((value >> 8) & 0xff);

    If your byte1 variable is a byte type, then this is not needed. But if the
    byte1 variable is a short or int, it could be needed depending on the
    values you are trying to shift.

    --
    Curt Welch http://CurtWelch.Com/
    http://NewsReader.Com/
     
    Curt Welch, Dec 5, 2007
    #7
  8. clinisbut

    Ian Shef Guest

    clinisbut <> wrote in news:291d0703-b1a8-487a-a9c2-
    :

    > What I want to do exactly is this:
    >
    > I need to split (in a binary way) an Int into two bytes.
    > Something like:
    >
    > int value : 340
    > same value in binary: 101010100
    >
    > convert this to 2 bytes:
    > byte1 = 2 ( 1 in binary );
    > byte2 = 84 ( 1010100 );
    >
    > I'm trying to do this but does not work:
    > byte1 = value>>8;
    > byte2 = value & 0x00FF;
    >
    > But gives me wrong values... What I'm doing wrong!?


    This question has been answered by others, so I won't repeat their answers.
    However, I will point out that:
    1) You are not splitting an Int, you are splitting an int.
    2) An int consists of four bytes. Splitting an int into two bytes will get
    you into trouble for negative numbers and any int larger than 32,767 or
    65,535, depending upon your point of view on the sign issues.
     
    Ian Shef, Jan 11, 2008
    #8
    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. Schnoffos
    Replies:
    2
    Views:
    1,236
    Martien Verbruggen
    Jun 27, 2003
  2. trey

    newbie: char* int and char *int

    trey, Sep 10, 2003, in forum: C Programming
    Replies:
    7
    Views:
    410
    Irrwahn Grausewitz
    Sep 10, 2003
  3. Hal Styli
    Replies:
    14
    Views:
    1,679
    Old Wolf
    Jan 20, 2004
  4. andrea_

    how to convert an int array to a char array

    andrea_, Sep 7, 2005, in forum: C Programming
    Replies:
    2
    Views:
    641
    Flash Gordon
    Sep 7, 2005
  5. Frank Liebelt

    Convert int array to char array

    Frank Liebelt, Sep 27, 2007, in forum: C Programming
    Replies:
    8
    Views:
    1,703
    Martin Ambuhl
    Sep 27, 2007
Loading...

Share This Page