java String 转化 byte 的一个问题

来源:百度知道 编辑:UC知道 时间:2024/05/29 12:26:07
String s = "1122";
现在想 得到 byte a = 0x11;byte b = 0x22;
怎么转化一下啊?
byte b = Byte.parseByte(s, 16);
String a = Integer.toHexString(b & 0xFF);
System.out.println(a);

这样就行了,,ok!

short sh = Short.parseShort (s, 16) ;

byte [] bytes = sh.toByteArray () ;

byte a = bytes [0] ;
byte b = bytes [1] ;

string型转byte 要怎么转 有两种方式
getBytes()
使用平台默认的字符集将此 String 解码为字节序列,并将结果存储到一个新的字节数组中。返回byte[]

getBytes(String charsetName)
使用指定的字符集将此 String 解码为字节序列,并将结果存储到一个新的字节数组中。返回
byte[]

1.String s = "1122"; byte a =Byte.parseByte(String s);
2.String s = "1122"; byte sbyte[]=s.getBytes();

不知道你要实现来用作什么。。实现上面有很多方法的。。能具体说下吗?