Two Laptop Bag
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

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Hex String to byte array" Watch "Hex String to byte array" New topic

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,
Rob Spoor

Joined: Oct 27, 2005
Posts: 20183

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

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:
subject: Hex String to byte array
It's not a secret anymore!