This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes byte array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "byte array" Watch "byte array" New topic
Author

byte array

Allan Cheong
Ranch Hand

Joined: Nov 12, 2009
Posts: 71

Hi all,

Are there any methods to convert the value of primitive data type to byte[]? Let's say, converting an int to byte[] will return byte[].length == 4, short to byte[] will return byte[].length == 2 and so on.

Is it possible doing it without using NIO?

Regards,
Allan Cheong


SCJP6, SCJD, OCJWCD5
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18509
    
  40

Allan Cheong wrote:Is it possible doing it without using NIO?


And the reason you don't want to use the ByteBuffer class is ... ??

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Allan Cheong
Ranch Hand

Joined: Nov 12, 2009
Posts: 71

I am doing SCJD assignment. NIO not allowed.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18509
    
  40

Allan Cheong wrote:I am doing SCJD assignment. NIO not allowed.


Then you are back to basics... For each of the four bytes, shift the bits into place, AND out the other bits, and assign (cast).

Henry
Allan Cheong
Ranch Hand

Joined: Nov 12, 2009
Posts: 71

Thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: byte array
 
Similar Threads
Comparing two byte arrays for equality
redirect problem
call something (set array and get its length ) from include file in jsp
Expressing unsigned integers and longs
Static Method call inside thread run