Hey guys and gals,

an int in java is 4 bytes as we all know i bet.

Trying to break my into 4 bytes :

int foo = 87;

bytes[4] mybyte = new bytes[4];

the problem is i don't really know how to break my int into single bytes
i tried this:
bytes[0] = foo | 1 <<(7-8); //trying to get bits 0-7 here, not working.

not good with shifting, if anyone can give me a boost, it will be much appreciated.

thank you,
Mel

Hi,

heres the way
mybyte[0] =(byte)( foo >> 24 );
mybyte[1] =(byte)( (foo << 8) >> 24 );
mybyte[2] =(byte)( (foo << 16) >> 24 );
mybyte[3] =(byte)( (foo << 24) >> 24 );

cheers,
aj.wh.ca

Yeah that works, even though it looks weird.:lol:
Thanks,
Mel

Hi,

heres the way
mybyte[0] =(byte)( foo >> 24 );
mybyte[1] =(byte)( (foo << 8) >> 24 );
mybyte[2] =(byte)( (foo << 16) >> 24 );
mybyte[3] =(byte)( (foo << 24) >> 24 );

cheers,
aj.wh.ca

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.