Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Convert int to char array (easy but...)

Reply
Thread Tools

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

 
 
clinisbut
Guest
Posts: n/a
 
      11-30-2007
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!?
 
Reply With Quote
 
 
 
 
Thomas Fritsch
Guest
Posts: n/a
 
      11-30-2007
clinisbut wrote:
> 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.

--
Thomas
 
Reply With Quote
 
 
 
 
Chris Dollin
Guest
Posts: n/a
 
      11-30-2007
clinisbut wrote:

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

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

 
Reply With Quote
 
Philipp
Guest
Posts: n/a
 
      11-30-2007
Thomas Fritsch wrote:
> clinisbut wrote:
>> 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.
>


You will also have to cast to byte...

int value = 340;
byte b1 = (byte)(value >> ;
byte b2 = (byte)(value & 0xFF);

 
Reply With Quote
 
Stefan Ram
Guest
Posts: n/a
 
      11-30-2007
clinisbut <(E-Mail Removed)> writes:
>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

 
Reply With Quote
 
clinisbut
Guest
Posts: n/a
 
      11-30-2007
On 30 nov, 14:47, Thomas Fritsch <(E-Mail Removed)> wrote:
> clinisbut wrote:
> > 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.
>
> --
> Thomas


Sorry, obviously I misstyped 1...
 
Reply With Quote
 
Curt Welch
Guest
Posts: n/a
 
      12-05-2007
clinisbut <(E-Mail Removed)> wrote:
> 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 >> & 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.

--
Curt Welch http://CurtWelch.Com/
http://www.velocityreviews.com/forums/(E-Mail Removed) http://NewsReader.Com/
 
Reply With Quote
 
Ian Shef
Guest
Posts: n/a
 
      01-11-2008
clinisbut <(E-Mail Removed)> wrote in news:291d0703-b1a8-487a-a9c2-
(E-Mail Removed):

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





 
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
Convert int array to char array Frank Liebelt C Programming 8 09-27-2007 11:08 AM
how to convert an int array to a char array andrea_ C Programming 2 09-07-2005 11:24 AM
int main(int argc, char *argv[] ) vs int main(int argc, char **argv ) Hal Styli C Programming 14 01-20-2004 10:00 PM
newbie: char* int and char *int trey C Programming 7 09-10-2003 03:24 AM
dirty stuff: f(int,int) cast to f(struct{int,int}) Schnoffos C Programming 2 06-27-2003 03:13 AM



Advertisments