`

byte与char数组互相转换

 
阅读更多
public static void main(String[] args) {

byte b = 17;
int i = 17;
char[] c = byte2char(b);

byte b2 = char2byte(c);

System.out.println(b2 == b);
}

public static char[] byte2char(byte b1) {

char[] c = new char[2];
// 高4位
c[0] = (char) ((b1 >> 4) & 0xf);
c[1] = (char) (b1 & 0xf);

return c;
}

public static byte char2byte(char[] c) {

byte high = (byte) ((c[0] << 4) | 0xf);

byte low = (byte) ((c[1]) | 0xf0);

return (byte) (low & high);
}

输出:true
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics