File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Give a byte, get a char,,, Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Give a byte, get a char,,," Watch "Give a byte, get a char,,," New topic
Author

Give a byte, get a char,,,

Landon Blake
Ranch Hand

Joined: Dec 04, 2003
Posts: 121
I'm working on a parser for a binary file format. I'll be getting ints from the Java reader that represent the values of each byte in the file being parsed. Many of these bytes represent ASCII text. I'm looking for the best way to:

[1] Convert a single int representing the value of a byte read from the binary file into a char or String that represents the corresponding ASCII value.

[2] Convert a group of ints representing the value of a series of bytes read from the binary file into a single String.

I think I can do #1 using the toString() method of the Byte class. Is this the best way? How would I accomplish number 2?

Thanks for the help.

Landon
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18113
    
    8

The best way to do both of them is to create a byte array from the input (perhaps containing only one element) and then to use the new String(byte[]) constructor. Both of the toString() methods in Byte (the static and instance methods) convert a byte to a String representing the binary value of that byte. That isn't what you want to do.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Give a byte, get a char,,,
 
Similar Threads
converting a string to bytes
NX: reading the Datafile
i/o streams and charsets...help
Understanding Byte Data and Character Encoding
Trouble with '.hasNext() '