可能重复:
Convert a hex string to a byte in Java将字母数字字符串转换为java中的字节?
在这里,我需要的字母数字字符串转换为字节的值,例如:
字符串str = “1b” 时到字节值我尝试使用getbytes
,(Byte.valueOf(str))
,(Byte.parseByte(str))
。
所有命令显示名为
java.lang.NumberFormatException
帮助,请
可能重复:
Convert a hex string to a byte in Java将字母数字字符串转换为java中的字节?
在这里,我需要的字母数字字符串转换为字节的值,例如:
字符串str = “1b” 时到字节值我尝试使用getbytes
,(Byte.valueOf(str))
,(Byte.parseByte(str))
。
所有命令显示名为
java.lang.NumberFormatException
帮助,请
异常假设你总是有代表十六进制值2个字符的字符串,你只是想:
byte b = Byte.parseByte(text, 16);
您需要指定16,以便它知道将其视为十六进制。
谢谢。现在我为我工作..... !!! @ – user1939336
如果是3个字符的字符串,该怎么办? – user1939336
@ user1939336:然后它不代表一个十六进制的单个字节,是吗? –
使用
的Byte.parseByte( “0x0B中”,16); 16:沙参
我希望这可以帮助你
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);
}
}
thankyou ..........! – user1939336
你期望得到的27的值,即0x1b? –