I am looking for a way to convert a long string (from a dump), that represents hex values into a byte array.
I couldn't have phrased it better than the person that posted the same question here.
But to keep it original, I'll phrase it my own way: suppose I have a string "00A0BF"
that I would like interpreted as the
byte[] {0x00,0xA0,0xBf}
what should I do?
I am a Java novice and ended up using BigInteger
and watching out for leading hex zeros. But I think it is ugly and I am sure I am missing something simple.
You can now use BaseEncoding in
guava
to accomplish this.To reverse it use
The
BigInteger()
Method from java.math is very Slow and not recommandable.Integer.parseInt(HEXString, 16)
can cause problems with some characters without converting to Digit / Integer
a Well Working method:
Function:
Have Fun, Good Luck
I like the Character.digit solution, but here is how I solved it
My formal solution:
Is like the PHP hex2bin() Function but in Java style.
Example:
One-liners:
Warnings:
eckes
)Fabian
for noting that), but you can just take the source code if your system lacksjavax.xml
for some reason. Thanks to @Bert Regelink
for extracting the source.The Hex class in commons-codec should do that for you.
http://commons.apache.org/codec/