public class HexUtil2 {
private static final 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'
};
public static String toString(int i, int radix, int length) {
final char[] buf = new char[length];
final int mask = radix - 1;
int charPos = length;
do {
buf[--charPos] = digits[i & mask];
i /= radix;
} while (i != 0);
while (charPos > 0) {
buf[--charPos] = '0';
}
return new String(buf, charPos, (length - charPos));
}
public static String toString(byte[] bytes, int radix, int length) {
final StringBuffer sb = new StringBuffer(bytes.length * length);
for (int i=0; i<bytes.length; i++) {
sb.append(toString(bytes[i] & 0xff, radix, length)); //自然数にしたバイト値を、16進数文字列に変換
}
return sb.toString();
}
public static byte[] toByteArray(String s, int radix, int length) {
final char[] buf = s.toCharArray();
final byte[] bytes = new byte[buf.length / length];
for (int i=0; i<buf.length/length; i++) {
bytes[i] = (byte)Integer.parseInt(new String(buf, i*length, length), radix);
}
return bytes;
}
2011/12/27
16進数文字列(String)⇔バイト配列(byte[]) プチ高速版
16進数文字列(String)⇔バイト配列(byte[])のプチ高速板を作成してみました。
登録:
コメントの投稿 (Atom)
0 件のコメント:
コメントを投稿