Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2005

Tip: Looking for answers? Try searching our database.

wierd cast from int to byte

Thread view: 
djbitchpimp@snowboard.com - 03 Oct 2005 10:53 GMT
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]?
Thomas Schodt - 03 Oct 2005 12:08 GMT
> but when I print them

Show us the code.
djbitchpimp@snowboard.com - 03 Oct 2005 17:51 GMT
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 [i] = 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 ;
       
}
Thomas Schodt - 03 Oct 2005 19:57 GMT
> byte encrypted [] = new byte [8] ;

You did not include the code where you print this...
djbitchpimp@snowboard.com - 03 Oct 2005 21:02 GMT
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 [i]) ;
        }

        //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;
    }
Steve Horsley - 03 Oct 2005 22:40 GMT
> byteArrToString(encrypted) ;
>
[quoted text clipped - 28 lines]
>         //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
Thomas Hawtin - 03 Oct 2005 14:22 GMT
> 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.
[quoted text clipped - 6 lines]
> 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
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/

Roedy Green - 03 Oct 2005 18:37 GMT
>I casted an 8 ints to byte types.

We need to see code to figure out what you mean.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.

djbitchpimp@snowboard.com - 03 Oct 2005 18:45 GMT
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
Roedy Green - 03 Oct 2005 18:49 GMT
>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
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.