Convert int to char array (easy but...)

C

clinisbut

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!?
 
T

Thomas Fritsch

clinisbut said:
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 );
byte1 = 1 ( 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!?
You calculated wrong byte1. See above.
I get byte1=1 and byte2=84.
 
C

Chris Dollin

clinisbut said:
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 );

2 != 1.

1 in binary is 1.
 
P

Philipp

Thomas said:
byte1 = 1 ( 1 in binary );

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);
 
S

Stefan Ram

clinisbut said:
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
 
C

Curt Welch

clinisbut said:
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!?

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.
 
I

Ian Shef

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!?

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,816
Latest member
SapanaCarpetStudio

Latest Threads

Top