IntelliJ Java IDE
The moose likes Java in General and the fly likes int to bytes Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "int to bytes" Watch "int to bytes" New topic
Author

int to bytes

John Ryan
Ranch Hand

Joined: Mar 14, 2001
Posts: 124
Hi all,
I had a post relating to this topic a few weeks back where i wanted to turn two bytes into a character. Now i want to turn a character into two bytes. I have seen this done by someone else and i have a quick question on it. I first cast my character to an int.
char charA = "A"
int a = ( int ) charA;
To put bits 8 to 15 into a byte i can do the following
byte firstByte = ( byte ) (a >> 8);
Then to put bits 7 to 0 into a byte i can do the following
byte secondByte = ( byte ) a;
As far as i can make out this gives me a correct solution.
Howerver i have seen a solution where the second byte is calculated as follows:
byte secondByte = ( byte )( a & ooff );
I dont understand the purpose of AND'ing the value of a by 256. Will casting a to a byte not give me the first 8 bits. The maximum value that can be stored in 8 bits is 256 so why AND it.
For example if the character 'A' were held in an int, that int would have the binary representation
01000001.
AND'ing this by 256 11111111 would leave me with 01000001, the original value.
So can anyone give me a reason for AND'ing with 256.
Cheers,
John

[ February 06, 2003: Message edited by: John Ryan ]
Chris Allen
Ranch Hand

Joined: Feb 01, 2003
Posts: 127
Just a shot in the dark here but I am wondering if the AND is performed in order to maintain the significant digit. In this example it won't matter because the character is always a positive integer but what about a case where you are trying to split a negative integer into two bytes? Either way, I think your solution is acceptable.
John Ryan
Ranch Hand

Joined: Mar 14, 2001
Posts: 124
Originally posted by Chris Allen:
Just a shot in the dark here but I am wondering if the AND is performed in order to maintain the significant digit. In this example it won't matter because the character is always a positive integer but what about a case where you are trying to split a negative integer into two bytes? Either way, I think your solution is acceptable.

Well when converting a character the int will never be negative so there would be no need to do this check
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 1900

I have seen (and use this myself) when converting numeric values (rather than character values) to byte for the reason as stated in the first reply. when converting a character to a byte I wouldn't see this a necessary. I don't know if this would be a problem if you use other special characters rather than just simple alpha-numerics however...


Steve
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1863
hi Steve,

I have seen (and use this myself) when converting numeric values (rather than character values) to byte for the reason as stated in the first reply


do u mean - "maintaining significant bit"? where do we maintain it? i don't see it. and in the same reply Chris mentions in the last that,

Either way, I think your solution is acceptable.

which means we don't need "&" useage.
if we r casiting something to byte thats it. we only consider least significant byte. why we care what the sign was? (anyways we are killing the person then why care about if he had two or one hand? )
let me know if i'm missing anything important here...
regards
maulin


1. Have fun @ http://faq.javaranch.com/java/JavaRaq
2. Looking for simple infix2postfix conversion and postfix evaluation package? Click here
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
The cause of the confusion is, that 0xff doesn't equal 256, but 255 instead. 255 represented in binary is 11111111. If you'd bitwise-and any integer with this value, the result would be the least significant byte of the original number.
Here's a small example:

but of course, casting an int to a byte type, as in 'byte lo= (byte)num' chops off all but the least significant byte also.
kind regards
 
 
subject: int to bytes
 
Threads others viewed
problem base64 encoding the 3 hex bytes E2 80 A9
bytes to char
Java & C++ pro question
complement in byte if range exceeds
unsigned integer
MyEclipse, The Clear Choice