Velocity Reviews > Java > int to byte array

# int to byte array

toffe
Guest
Posts: n/a

 04-06-2004
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

Babu Kalakrishnan
Guest
Posts: n/a

 04-06-2004
toffe wrote:
>
> 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

Chris Smith
Guest
Posts: n/a

 04-06-2004
Babu Kalakrishnan wrote:
> 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

toffe
Guest
Posts: n/a

 04-08-2004
Babu Kalakrishnan wrote:
> toffe wrote:
>
>>
>> I need an efficient way of turning an int into a byte array.

> 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

Dave Monroe
Guest
Posts: n/a

 04-09-2004
toffe <(E-Mail Removed)> wrote in message news:<vVwcc.88868\$(E-Mail Removed)>...
> 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

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

Dave Monroe