KeksArmee
KeksArmee

Reputation: 1429

Java: Convert a hexadecimal encoded String to a hexadecimal byte

An Item-ID in hexadecimal and the amount in decimal has to be entered in two JTextFields.

Now I have to convert the Item ID hexadecimal encoded in a String to a byte hexadecimal.

String str = itemIdField.getText(); // Would be, for example, "5e"
byte b = // Should be 0x5e then.

So if str = "5e", b = 0x5e

if str = "6b" b = 0x6b and so on.

Does anybody now, what the code to convert that would be then? Google doesn't know, it thinks, I want to convert the text to a byte[]

Thank you, Richie

Upvotes: 2

Views: 221

Answers (1)

tofcoder
tofcoder

Reputation: 2382

You can use Byte.parseByte(str, 16), that will return the byte value represented by the hexadecimal value in str.

Upvotes: 4

Related Questions