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!?
Thomas Fritsch - 30 Nov 2007 13:47 GMT
> What I want to do exactly is this:
>
[quoted text clipped - 6 lines]
> convert this to 2 bytes:
> byte1 = 2 ( 1 in binary );
byte1 = 1 ( 1 in binary );
> byte2 = 84 ( 1010100 );
>
[quoted text clipped - 3 lines]
>
> But gives me wrong values... What I'm doing wrong!?
You calculated wrong byte1. See above.
I get byte1=1 and byte2=84.

Signature
Thomas
Philipp - 30 Nov 2007 14:04 GMT
>> What I want to do exactly is this:
>>
[quoted text clipped - 17 lines]
> 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);
clinisbut - 30 Nov 2007 18:15 GMT
> > What I want to do exactly is this:
>
[quoted text clipped - 22 lines]
> --
> Thomas
Sorry, obviously I misstyped 1...
Chris Dollin - 30 Nov 2007 14:00 GMT
> What I want to do exactly is this:
>
[quoted text clipped - 6 lines]
> convert this to 2 bytes:
> byte1 = 2 ( 1 in binary );
2 != 1.
1 in binary is 1.

Signature
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
Stefan Ram - 30 Nov 2007 15:39 GMT
>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
Curt Welch - 05 Dec 2007 02:39 GMT
> What I want to do exactly is this:
>
[quoted text clipped - 13 lines]
>
> 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.

Signature
Curt Welch http://CurtWelch.Com/
curt@kcwc.com http://NewsReader.Com/
Ian Shef - 11 Jan 2008 19:11 GMT
clinisbut <clinisbut@gmail.com> wrote in news:291d0703-b1a8-487a-a9c2-
11bd035aa012@i12g2000prf.googlegroups.com:
> What I want to do exactly is this:
>
[quoted text clipped - 13 lines]
>
> 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.