int to byte array

T

toffe

Hi all,

I need an efficient way of turning an int into a byte array.

Examples:

int byte array
1) 0 -> {0}
2) 1 -> {1}
3) 256 -> {1,0}
4) 70000 -> {1,17,112} (since 70000 = 1 * 256^2 + 17 * 256 + 112)


I can think of a few more or less odd ways to do this,
but I need an efficient one. I suspect that there is some function
already built in into Java that does this very thing.

Any help on this one?

Cheers,
toffe
 
B

Babu Kalakrishnan

toffe said:
I need an efficient way of turning an int into a byte array.

Examples:

int byte array
1) 0 -> {0}
2) 1 -> {1}
3) 256 -> {1,0}
4) 70000 -> {1,17,112} (since 70000 = 1 * 256^2 + 17 * 256 + 112)


I can think of a few more or less odd ways to do this,
but I need an efficient one. I suspect that there is some function
already built in into Java that does this very thing.

Strictly speaking, this isn't even possible because a "byte" in java is a signed
entity - so values greater than 127 aren't allowed (whereas you actually need to
store unsigned numbers between 0 and 255).

In any case, the easiest (and probably most efficient way) of doing this is to
right shift the int value by 0,8,16 and 24 bits and "and"ing with 0xFF.

BK
 
C

Chris Smith

Babu said:
Strictly speaking, this isn't even possible because a "byte" in java is a signed
entity - so values greater than 127 aren't allowed (whereas you actually need to
store unsigned numbers between 0 and 255).

I've sometimes found it easier to deal with data under the model that
the signed-ness of a value depends on the operation rather than the
value's type. Operations can then be grouped into three categories:
signed, unsigned, and "doesn't matter". For the most part, it's then
possible to deal with signed 8-bit byte values by avoiding the class of
"unsigned" operations, and vice versa.

Since all standard API string conversion routines fall into the
"signed" category, bytes definitely look signed, but don't have to be
treated that way. There are, however, a few "signed" operations that
have no unsigned equivalent. In those cases, it's necessary to convert
to a larger data type for the temporary result.

This model is less consistent with the vocabulary of most reference
sources and the JLS, but is equally effective at explaining the behavior
of the language.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
T

toffe

Babu said:
right shift the int value by 0,8,16 and 24 bits and "and"ing with 0xFF.

BK

Thank you very much. It worked (I can pretend the bytes are unsigned).

-toffe
 
D

Dave Monroe

toffe said:
Hi all,

I need an efficient way of turning an int into a byte array.

Examples:

int byte array
1) 0 -> {0}
2) 1 -> {1}
3) 256 -> {1,0}
4) 70000 -> {1,17,112} (since 70000 = 1 * 256^2 + 17 * 256 + 112)


I can think of a few more or less odd ways to do this,
but I need an efficient one. I suspect that there is some function
already built in into Java that does this very thing.

Any help on this one?

Cheers,
toffe


How about:
int x = 7000;
String s = x + "";
byte [] barr = s.getBytes();

Dave Monroe
 

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

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top