Java各个类型与byte[]的转换
int 转 byte[]
int temp = v; byte[] bt = new byte[4]; for (int i = 0; i < bt.length; i++) { // 将最低位保存在最低位 例如90 输出[5A][00][00][00] bt[i] = new Integer(temp & 0xff).byteValue(); temp = temp >> 8; // 向右移8位 }
byte[] 转 int
byte[] data = xxx; byte bLoop; int result= 0; for (int i = 0; i < data.length; i++) { bLoop = data[i]; result+= (bLoop & 0xFF) << (8 * i); }
String 转 byte[]
String s=new String("测试"); byte[] wrbyte = s.getBytes(); byte[] 转 String byte[] data = cmd.getData(); try { String s = new String(data,"utf-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); }