把从SD卡里取出来的字符串“0x03 0x04 0x05"转为(byte)0x03 (byte)0x03
public static byte[] hexStringToBytes(String hexString) {
if (hexString == null || hexString.equals("")) {
return null;
}
hexString = hexString.toUpperCase();
if(hexString.indexOf("0X")>=0) hexString=hexString.replace("0X", "");
int length = hexString.length() / 2;
char[] hexChars = hexString.toCharArray();
byte[] d = new byte[length];
for (int i = 0; i < length; i++) {
int pos = i * 2;
d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
}
return d;
}
private static byte charToByte(char c) {
return (byte) "0123456789ABCDEF".indexOf(c);
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
byte[] by = hexStringToBytes("0x030x040x05");
byte[] by2 = new byte[]{(byte) 0x03,(byte) 0x04,(byte) 0x05};
//这两个by和by2是一致的
}
这。。 写个小程序呗
String[] s = “0x03 0x04 0x05".split(" ");
byte[] byte = =new byte[s.size];
int i = 0;
for(String str: s) {
byte[i] = str;
i++;
}
16进制转为字节,不知道这个行不
http://my.oschina.net/u/1024767/blog/178960
http://www.cnblogs.com/ruiati/p/3706438.html
这个是典型的ascii到hex的转换,需要自己写个函数来实现,毕竟ascii的格式不确定