Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > wierd cast from int to byte

Reply
Thread Tools

wierd cast from int to byte

 
 
djbitchpimp@snowboard.com
Guest
Posts: n/a
 
      10-03-2005
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]?

 
Reply With Quote
 
 
 
 
Thomas Schodt
Guest
Posts: n/a
 
      10-03-2005
wrote:

> but when I print them


Show us the code.
 
Reply With Quote
 
 
 
 
Thomas Hawtin
Guest
Posts: n/a
 
      10-03-2005
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/
 
Reply With Quote
 
djbitchpimp@snowboard.com
Guest
Posts: n/a
 
      10-03-2005
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 ;

}

 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      10-03-2005
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.
 
Reply With Quote
 
djbitchpimp@snowboard.com
Guest
Posts: n/a
 
      10-03-2005
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

 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      10-03-2005
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.
 
Reply With Quote
 
Thomas Schodt
Guest
Posts: n/a
 
      10-03-2005
wrote:
> byte encrypted [] = new byte [8] ;


You did not include the code where you print this...
 
Reply With Quote
 
djbitchpimp@snowboard.com
Guest
Posts: n/a
 
      10-03-2005
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;
}

 
Reply With Quote
 
Steve Horsley
Guest
Posts: n/a
 
      10-03-2005
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 [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;
> }
>


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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
cast from int** to int* Praveen Raj C Programming 42 07-09-2009 07:14 AM
Re: byte + byte -> int Lew Java 6 03-21-2009 02:22 AM
Alternative for int ** to const int ** cast? Randall Parker C++ 4 05-19-2006 06:56 PM
int main(int argc, char *argv[] ) vs int main(int argc, char **argv ) Hal Styli C Programming 14 01-20-2004 10:00 PM
dirty stuff: f(int,int) cast to f(struct{int,int}) Schnoffos C Programming 2 06-27-2003 03:13 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57