File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Hex String to 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 "Hex String to byte array" Watch "Hex String to byte array" New topic
Author

Hex String to byte array

Titus Abraham
Ranch Hand

Joined: Jun 03, 2009
Posts: 36
I am puzzled,

I am trying to convert a hex string which i get from network but i am decoding it different everytime. I have no idea why. i am pasting the function which i am using .

Th Hex string is "827000000d5e"



any idea why ... I have been puzzling on it for 2 hours now..


any help appreciated..

best regards,
Titus
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19785
    
  20

Please Use Code Tags.

Some hints:
What if no.length() is odd? You're missing out one digit. Try the following:
That also ensures your string always has an even number of characters.
The "& 0x000000ff" part is unnecessary - the cast to byte cuts off anything over 0x000000ff anyway. But it doesn't hurt though.

Your second method can be removed, because the first method does the exact same thing - take a HEX string and create a byte[]. Why use two different methods? And the first one works (after the odd-length-fix) - I know because it's identical to my own HEX-string-to-byte method


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Titus Abraham
Ranch Hand

Joined: Jun 03, 2009
Posts: 36
Thank you...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Hex String to byte array