• Post Reply Bookmark Topic Watch Topic
  • New Topic

Convert string to hex bytes  RSS feed

 
Jim Venolia
Ranch Hand
Posts: 312
2
Chrome Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've got a text string representing hex data, and I need to convert it to an array of bytes.  For example, I need to convert "36420f96"  to an array containing [0x36, 0x42, 0x0f, 0x96].  Using String.getBytes yields the array [0x33, 0x36, 0x34, 0x32, 0x30 etc etc], which is not what I want.

The input string can be long, as in I'm not aware of an upper limit to it's size.  My test string is 40 characters or so.

I could write this routine easily, but it seems to me there should be a method in some class somewhere to do what I need.  I've been reading up on CharsetEncoder but I'm not seeing how it helps me.

Ideas?
 
Jim Venolia
Ranch Hand
Posts: 312
2
Chrome Linux VI Editor
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got it.  byte[] theHexData = DatatypeConverter.parseHexBinary(input);

 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!