62,634
社区成员




public class Test {
final static char[] digits = { '0', '1', '2', '3', '4', '5', '6', '7', '8',
'9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y',
'z' };
private static String toHexString(int i) {
char[] buf = new char[32];
int charPos = 32;
int radix = 1 << 4;
int mask = radix - 1;
do {
buf[--charPos] = digits[i & mask];
i >>>= 4;
} while (i != 0);
return new String(buf, charPos, (32 - charPos));
}
public static void main(String[] args) {
int i = 55;
System.out.println(toHexString(i));
}
}
public class ByteUtil {
private final static char[] HEX = "0123456789abcdef".toCharArray();
private ByteUtil() { }
public static String bytes2StrSpace(byte[] bys) {
char[] chs = new char[bys.length * 3 - 1];
for(int i = 0, k = 0; i < bys.length; i++) {
if(k > 0) {
chs[k++] = ' ';
}
chs[k++] = HEX[(bys[i] >> 4) & 0xf];
chs[k++] = HEX[bys[i] & 0xf];
}
return new String(chs);
}
public static String bytes2Str(byte[] bys) {
char[] chs = new char[bys.length * 2];
for(int i = 0, k = 0; i < bys.length; i++) {
chs[k++] = HEX[(bys[i] >> 4) & 0xf];
chs[k++] = HEX[bys[i] & 0xf];
}
return new String(chs);
}
public static String byte2Str(byte b) {
char[] chs = new char[2];
chs[0] = HEX[(b >> 4) & 0xf];
chs[1] = HEX[b & 0xf];
return new String(chs);
}
}