2

Possible Duplicate:
Convert a hex string to a byte in Java

Here, I need to convert alphanumeric String to byte value ,for example:

String str ="1b" to byte value.I tried by using getbytes,(Byte.valueOf(str)), (Byte.parseByte(str)).

All the commands showed an exception called

  java.lang.NumberFormatException

help please

Community
  • 1
  • 1
user1939336
  • 31
  • 2
  • 7

3 Answers3

3

Assuming you're always going to have a 2-character string representing a hex value, you just want:

byte b = Byte.parseByte(text, 16);

You need to specify the 16 so that it knows to treat it as hex.

Jon Skeet
  • 1,335,956
  • 823
  • 8,931
  • 9,049
2

i hope this may help you

public class TestByte
{    
    public static void main(String[] argv) {

            String example = "example100";
            byte[] bytes = example.getBytes();

            System.out.println("Text : " + example);
            System.out.println("Text [Byte Format] : " + bytes);


    }
}
Mr.Cool
  • 1,497
  • 10
  • 31
  • 49
1

using

Byte.parseByte("0x0b", 16); 16:radix

Daniel
  • 79
  • 3