aspose file tools*
The moose likes Beginning Java and the fly likes To Hex String and back... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "To Hex String and back..." Watch "To Hex String and back..." New topic
Author

To Hex String and back...

Marcus Laubli
Ranch Hand

Joined: Dec 24, 2004
Posts: 116
I'm in the middle of a muddle....

I'm trying to create a small class that will give me tools to convert a String to a string of hex numbers, then take that string of hex numbers and return to a normal string.

Here's my code:




Right now, I'm just worried about the toHexString method and the hexToString method. In my main method, I first convert with:



then I want to return to my original string with



Here's my output..



As you can see, the result from hexToString does not result in the abc123 that went in at the beginning.

I think the problem is the casting of rawInt to (char), but I don't know how to change this!

Please help.
[ May 11, 2005: Message edited by: Marcus Laubli ]

Marcus L´┐Żubli, SCJP 1.4, CLP 5.0, SCWCD 1.4 (preparing)
M Beck
Ranch Hand

Joined: Jan 14, 2005
Posts: 323
in hexToString, you seem to be doing:

but will that take into account the radix of the intString? your example is "abc123", which appears to come out to a hexadecimal string without any of the letters A-F in it, but it's still base 16, correct?
Marcus Laubli
Ranch Hand

Joined: Dec 24, 2004
Posts: 116
I just figured out what's happening. I get Hex 61 for 'a', which is the first letter in the string. Then when I convert 61 back, I'm hitting decimal 61, which is '='... oops.

How can I change this? I'm guessing that I really only need 00 - 7F in hex.
Marcus Laubli
Ranch Hand

Joined: Dec 24, 2004
Posts: 116
Just changed the output in the first message because I ran it through twice. Sorry for the confusion!
M Beck
Ranch Hand

Joined: Jan 14, 2005
Posts: 323
well, looking through the API docs for the Integer class, i notice there's a different version of parseInteger that seems like it might be useful in this case...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: To Hex String and back...
 
Similar Threads
How to format a byte as %02X?
Midi file to Hex ASCII conversion
Language doubt
Right shift using array
Bit wise ,shift Operators practical usage