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 Convert Hex byte to ASCII Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Convert Hex byte to ASCII" Watch "Convert Hex byte to ASCII" New topic
Author

Convert Hex byte to ASCII

John C Clark
Greenhorn

Joined: Jun 03, 2004
Posts: 4
I am reading an array of bytes. I want to translate each hexadecimal number into it's ASCII value. How can I do this?

The array is of type byte, because I am using RandomAccessFile to read the input from a source file. When I try to call byteVariable.toString() method, I get an error "cannot dereference" at compile time. I suppose this error would not occur if it was of type Byte, but when I change the type of my array variable to Byte, I get an error from RandomAccessFile class, it seems like it wants type byte.

I am new to Java so I am not copmletely sure what is going on.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24184
    
  34

Hi,

Welcome to JavaRanch!

First off, you must've overlooked our naming policy when you sign up. We need to see real first and last names. You can fix it here. Thanks, pardner!

Now, as to your question: I suppose the right answer depends on exactly what you want to do with the "ASCII value." I'm going to guess "Print it as a number from 0 to 255". In that case, using the bitwise "and" operator and an appropriate bitmask is the right way to convert the byte to a non-negative integer:



prints "219".
[ June 03, 2004: Message edited by: Ernest Friedman-Hill ]

[Jess in Action][AskingGoodQuestions]
John C Clark
Greenhorn

Joined: Jun 03, 2004
Posts: 4
Thanks for your quick reply and I apologize for my name.

My output now is: 48 48 48 49 50

I want it to print: H H H I P

The solution you provided did not work for me. It still printed the same values.

Also, is it possible to convert a byte to a string?
[ June 03, 2004: Message edited by: John Clark ]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24184
    
  34

Originally posted by John Clark:


I want it to print: H H H I P


You've got the ASCII (really Unicode, but close enough) code as an integer; just cast it to a character:

System.out.println((char) b && 0xFF);

Now, as far as converting bytes to a String, yes, sure. Why not give me a slightly bigger picture of what you're trying to do, and I can tell you the best way to proceed.
David Weitzman
Ranch Hand

Joined: Jul 27, 2001
Posts: 1365
If you want to convert a byte array of ASCII characters into a string, you can use one of the String constructors:



Just watch out for the checked UnsupportedEncodingException if your computer doesn't support ASCII (ack!).
John C Clark
Greenhorn

Joined: Jun 03, 2004
Posts: 4
I read a file that has a sequence of numbers in it. I read the sequence using RandomAccessFile. In my byte array are the values 4848484950

So, currently when I print to the screen it prints
48
48
48
49
50

I want it to print
H
H
H
I
P

Below is my code.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Convert Hex byte to ASCII