wierd cast from int to byte

Discussion in 'Java' started by djbitchpimp@snowboard.com, Oct 3, 2005.

  1. Guest

    I casted an 8 ints to byte types. Some of them come out in the right
    format, but when I print them, some come out way bigger than 8 bits.

    Here is the string representation of the byte [8] array:

    1111111111111111111111110000101 1111111111111111111111111101000 10011
    1010100 1111 1010 1111111111111111111111110110100 101

    How can I truncate off the extra 1's to just get the last 8 bits of
    values [0], [1], and [6]?
    , Oct 3, 2005
    #1
    1. Advertising

  2. wrote:

    > but when I print them


    Show us the code.
    Thomas Schodt, Oct 3, 2005
    #2
    1. Advertising

  3. wrote:
    > I casted an 8 ints to byte types. Some of them come out in the right
    > format, but when I print them, some come out way bigger than 8 bits.
    >
    > Here is the string representation of the byte [8] array:
    >
    > 1111111111111111111111110000101 1111111111111111111111111101000 10011
    > 1010100 1111 1010 1111111111111111111111110110100 101
    >
    > How can I truncate off the extra 1's to just get the last 8 bits of
    > values [0], [1], and [6]?


    All numerical types are signed (except char). Before any arithmetic on
    byte, char or short is performed the value is transformed into an int.

    Consider the byte -1, which is 11111111 in binary. If you wanted to see
    if bit n of byte b is set you might try (b & (1<<n)) != 0. If b is
    11111111 (binary) then it is first converted to the int
    11111111111111111111111111111111 (binary). So, you need to either remove
    or ignore the top 24 bits.

    You can remove all but the bottom eight bits of an int, i, with i &
    0xff. So to get an unsigned binary representation of a byte, b, you can
    use Integer.toBinaryString(b & 0xff).

    Tom Hawtin
    --
    Unemployed English Java programmer
    http://jroller.com/page/tackline/
    Thomas Hawtin, Oct 3, 2005
    #3
  4. Guest

    byte encrypted [] = new byte [8] ;
    int encryptedInt [] = new int [] = {
    1,0,0,0,0,1,0,1,1,1,1,0,1,0,0,0,0,0,0,1,0,0,1,1,
    0,1,0,1,0,1,0,0,0,0,0,0,1,1,1,1,0,0,0,0,1,0,1,01,0,1,1,0,1,0,0,
    0,0,0,0,0,1,0,1 };

    // convert encryptedInt back to binary
    int pack [] = new int [8] ;

    for (i = 0; i < 8; i++) { //each individual byte
    for (int j = 0; j < 8; j++) { // each bit in that byte
    pack [j] = encryptedInt [j+(8*i)] ;
    }
    encrypted = packByte (pack) ;
    }

    private byte packByte (int [] toPack) {
    int byteVal = 0 ;
    int temp = 0 ;
    int i ;

    byteVal = toPack[0] & 0x1 ;
    byteVal <<= 7 ;
    for (i = 7; i > 0; i--) {

    temp = toPack [8 - i] & 0x1 ;
    //System.out.print ("Bit " + (8 - i) + " " + temp + " ") ;
    temp <<= i - 1 ;
    //System.out.println (temp) ;
    byteVal = byteVal | temp ;
    byteVal = byteVal & 0xff ;
    //System.out.println (byteVal) ;
    }

    // byteval has the value of the byte
    return (byte) byteVal ;

    }
    , Oct 3, 2005
    #4
  5. Roedy Green Guest

    On 3 Oct 2005 02:53:11 -0700, wrote or
    quoted :

    >I casted an 8 ints to byte types.


    We need to see code to figure out what you mean.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Again taking new Java programming contracts.
    Roedy Green, Oct 3, 2005
    #5
  6. Guest

    I just posted some code in reply to Thomas. When I print out the value
    of the byte [] array in binary:

    1111111111111111111111110000101 1111111111111111111111111101000 10011
    1010100 1111 1010 1111111111111111111111110110100 101
    , Oct 3, 2005
    #6
  7. Roedy Green Guest

    On 3 Oct 2005 02:53:11 -0700, wrote or
    quoted :

    >How can I truncate off the extra 1's to just get the last 8 bits of
    >values [0], [1], and [6]?


    see http://mindprod.com/jgloss/masking.html
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Again taking new Java programming contracts.
    Roedy Green, Oct 3, 2005
    #7
  8. wrote:
    > byte encrypted [] = new byte [8] ;


    You did not include the code where you print this...
    Thomas Schodt, Oct 3, 2005
    #8
  9. Guest

    byteArrToString(encrypted) ;

    /* prints out the binary representation of a byte array */
    public static void byteArrToString (byte [] toPrint) {
    for (int i = 0; i < toPrint.length; i++) {
    printBinary(toPrint ) ;
    }

    //System.out.println () ;
    }

    // Takes an int as an argument and prints it in binary.
    // Returns the length of the number in binary.
    // if boolean is false, it won't print anything.

    public static void printBinary(byte value) {
    boolean shouldPrint = true;
    int hasPrinted = 0;
    int pos = 32;
    while (pos > 0) {
    if (checkBit(value, pos)) {
    System.out.print(shouldPrint ? "1" : "");
    hasPrinted++;
    } else if (hasPrinted > 0) {
    System.out.print(shouldPrint ? "0" : "");
    hasPrinted++;
    }
    pos--;
    }
    System.out.print(shouldPrint ? " " : "");
    //return hasPrinted;
    }
    , Oct 3, 2005
    #9
  10. wrote:
    > byteArrToString(encrypted) ;
    >
    > /* prints out the binary representation of a byte array */
    > public static void byteArrToString (byte [] toPrint) {
    > for (int i = 0; i < toPrint.length; i++) {
    > printBinary(toPrint ) ;
    > }
    >
    > //System.out.println () ;
    > }
    >
    > // Takes an int as an argument and prints it in binary.
    > // Returns the length of the number in binary.
    > // if boolean is false, it won't print anything.
    >
    > public static void printBinary(byte value) {
    > boolean shouldPrint = true;
    > int hasPrinted = 0;
    > int pos = 32;
    > while (pos > 0) {
    > if (checkBit(value, pos)) {
    > System.out.print(shouldPrint ? "1" : "");
    > hasPrinted++;
    > } else if (hasPrinted > 0) {
    > System.out.print(shouldPrint ? "0" : "");
    > hasPrinted++;
    > }
    > pos--;
    > }
    > System.out.print(shouldPrint ? " " : "");
    > //return hasPrinted;
    > }
    >


    You don't post the code for checkBit(byte), but I guess that your
    problem is actually this line:
    int pos = 32;
    This explicitly asks to print bit values from position 32
    downwards. Starting with pos=8 shoudl fix it.

    You seem not to know about promotion and sign extension. Almost
    any operation on a byte (including bitwise operations) first
    promotes the byte to an int value and then performs the
    operation. As part of that promotion, a negative byte (e.g. -1 =
    11111111) has the extra ones added to keep the same value as an
    int (-1 = 11111111111111111111111111111111).

    HTH
    Steve
    Steve Horsley, Oct 3, 2005
    #10
    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,193
    Martien Verbruggen
    Jun 27, 2003
  2. Hal Styli
    Replies:
    14
    Views:
    1,608
    Old Wolf
    Jan 20, 2004
  3. Randall Parker
    Replies:
    4
    Views:
    470
    Jack Saalweachter
    May 19, 2006
  4. Lew

    Re: byte + byte -> int

    Lew, Mar 20, 2009, in forum: Java
    Replies:
    6
    Views:
    352
    Arne Vajhøj
    Mar 21, 2009
  5. Praveen Raj

    cast from int** to int*

    Praveen Raj, Jul 3, 2009, in forum: C Programming
    Replies:
    42
    Views:
    1,314
    Keith Thompson
    Jul 9, 2009
Loading...

Share This Page