1

I have a String str = "0x01". I want to convert this string into byte exactly as it is.

I tried doing str.getBytes() and EncodingUtils.getAsciiBytes(str) but it gives byte value of each character I suppose.

I want byte variable to have value as 0x01.

Any idea how do we do it?

Thanks

1 Answer 1

5

Try this:

byte b = Byte.decode("0x01");
Sign up to request clarification or add additional context in comments.

2 Comments

I went originally with Integer because I wasn't sure Byte.decode existed; there are/have been minor differences between the wrapper class features.
I see. Might depend on the API version.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.