首页 > 代码库 > 关于字节的处理方案
关于字节的处理方案
- 在Java中通过websocket得到的json数据需要进行对应的转化;
/**
*
* @Title: hexStringToBytes
* @Description: 将十六进制字符串转化为十六进制byte[]
* @param hexString
* @return
* @return: byte[]
*/
public static byte[] hexStringToBytes(String hexString) {
if (hexString == null || hexString.equals("")) {
return null;
}
hexString = hexString.toUpperCase();
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);
}
- 高低字节的截取
//取高八位
public static int getHighByte(int num){
return 0xffff&(num>>8);
}
//取低八位
public static int getLowByte(int num){
return (0xffff&(num<<8))>>8;
}
关于字节的处理方案