String address="00-15-f2-65-ec-59";
String[] split=address.split("\\-");
byte src[] = new byte[] { (byte) 0x00, (byte) 0x15,(byte) 0xf2, (byte) 0x65, (byte) 0xec,(byte)0x59 };如何把上面的split中的值转换成src中的值?即00→0x00,15→0x15
String[] split=address.split("\\-");
byte src[] = new byte[] { (byte) 0x00, (byte) 0x15,(byte) 0xf2, (byte) 0x65, (byte) 0xec,(byte)0x59 };如何把上面的split中的值转换成src中的值?即00→0x00,15→0x15
如果是的话,使用DecimalFormat类
例:(希望可以帮到你)InputStreamReader isr=new InputStreamReader(System.in);//把读入字节流转为字符流.
BufferedReader br=new BufferedReader(isr);//放入缓冲区
String s=br.readLine();//读出该字符流.
DecimalFormat df=new DecimalFormat();//创建一个DecimalFormat对象.
Number n=df.parse(s);//调用df的parse方法,将S格式化为十进制数字,返回给n.
byte b=n.byteValue();//调用Number对象n的byteValue方法,返回成为byte类型.
System.out.println(Integer.parseInt(str, 16));
}
{
String ret = "";
for (int i = 0; i < b.length; i++) {
String hex = Integer.toHexString(b[i]& 0xFF);
if (hex.length() == 1) {
hex = '0' + hex;
}
ret += hex.toUpperCase();
}
return ret;
}