我找到了一个将byte或char转换为十六进制值的java类。但我无法清楚地理解代码。你能解释一下代码的功能吗?或者我可以在哪里找到更多的资源?位移并将字符转换为Unicode转义字符串
public class UnicodeFormatter {
static public String byteToHex(byte b) {
// Returns hex String representation of byte b
char hexDigit[] = {
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
};
char[] array = {hexDigit[(b >> 4) & 0x0f], hexDigit[b & 0x0f]};
return new String(array);
}
static public String charToHex(char c) {
// Returns hex String representation of char c
byte hi = (byte) (c >>> 8);
byte lo = (byte) (c & 0xff);
return byteToHex(hi) + byteToHex(lo);
}
} // class
我不知道剩下的..但确实>>但是通过许多位的移位。所以b >> 4除以2^4(或16)。 – varatis