16进制跟10进制互转
10==>16 Integer.toHexString(10);
16==>10 Integer.parseInt("7F");
int data= 0x00000708;
ByteBuffer buffer = ByteBuffer.allocate(4);
buffer.putInt(data);
int byte2 = buffer.getShort(2); 两个字节值(111 00001000=1800);
16进制跟数组互转
public static byte[]hexStringToByteArray(String s) {//16进制String 转为数组
int len = s.length();
byte[] data =new byte[len /2];
for (int i =0; i < len; i +=2) {
data[i /2] = (byte) ((Character.digit(s.charAt(i), 16) <<4) + Character.digit(s.charAt(i+1), 16));
}
return data;
}
public static StringbytesToHexString(byte[] bArray) {//数组转为16进制String
StringBuffer sb =new StringBuffer(bArray.length);
for (int i =0; i < bArray.length; ++i) {
String sTemp = Integer.toHexString(255 & bArray[i]);
if (sTemp.length() <2) {
sb.append(0);
}
sb.append(sTemp.toUpperCase());
}
return sb.toString();
}
高四位值 :1&0xf0>>4
低四位值:1&0x0f
指定位值:
for (int i =0; i <8; i++) {
Log.e("getBit", getBit(127, i) +"==");
}
public int getBit(int value, int index) {
if (index >7)
return -1;
return value >> index &0x01;
}
网友评论