Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2006

Tip: Looking for answers? Try searching our database.

INT to BYTE

Thread view: 
Varun Gupta - 21 Nov 2006 12:31 GMT
How can we convert a primitive java int to Java byte.
byte and not byte[].

Waiting for replies.
Jan Thomä - 21 Nov 2006 13:07 GMT
int myInt = 65;

byte myByte = (byte) (myInt & 0xFF);

Greetings,
Jan

> How can we convert a primitive java int to Java byte.
> byte and not byte[].
>
> Waiting for replies.

Signature

__________________________________________________________
insOMnia - We never sleep...
http://www.insomnia-hq.de

rxreyn3@gmail.com - 25 Nov 2006 06:38 GMT
There is a commons library that makes conversion very easy:

http://jakarta.apache.org/commons/codec/

Ryan

> How can we convert a primitive java int to Java byte.
> byte and not byte[].
>
> Waiting for replies.
EJP - 26 Nov 2006 05:19 GMT
> There is a commons library that makes conversion very easy:
>
> http://jakarta.apache.org/commons/codec/

.. which does many things but that's one one of them.

A simple cast is all that's required, as already posted, but the '&
0xff' is not required. The cast does that already.
rxreyn3@gmail.com - 26 Nov 2006 05:23 GMT
Hmm, thought I did the same thing using the codec lib.  Of course, my
brain is %90 malted hops and bong resin; so thanks for correcting me.

- Ryan

> > There is a commons library that makes conversion very easy:
> >
[quoted text clipped - 4 lines]
> A simple cast is all that's required, as already posted, but the '&
> 0xff' is not required. The cast does that already.
sgoo - 26 Nov 2006 15:26 GMT
> A simple cast is all that's required, as already posted, but the '&
> 0xff' is not required. The cast does that already.

It *is* required if you want the output be between 0 and 255.
sgoo - 26 Nov 2006 15:28 GMT
Sorry, I thought it was from byte->int. It's not necessary here.

> > A simple cast is all that's required, as already posted, but the '&
> > 0xff' is not required. The cast does that already.It *is* required if you want the output be between 0 and 255.
Chris Uppal - 26 Nov 2006 15:43 GMT
> > A simple cast is all that's required, as already posted, but the '&
> > 0xff' is not required. The cast does that already.
>
> It *is* required if you want the output be between 0 and 255.

Depends on whether you want to mask an integer value down to a byte value, i.e.
a value between 0 and 255 inclusive; or whether you want to put the resulting
value into a Java byte variable (or array slot) which holds values in the
range -128 to +127 inclusive.  In the former case you should mask with 0xFF
(but don't cast, leave the result as an int); in the later case you just cast
(masking will gain exactly nothing).

   -- chris


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.