aspose file tools*
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
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: 18842
    
  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: 18842
    
  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