16 진 관련 조작
6151 단어 16 진법16 진 관련 조작
/**
* 16
*
* @author chenxin
* @version [ , 2012-5-21]
* @see [ / ]
* @since [ / ]
*/
public class 16Util {
private static final char[] HEX = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
/**
* int 16
*
* @param l
* @return 16
*/
public static String getHexStr(long l)
{
StringBuffer sbRst = new StringBuffer(20);
int idx = (int) ((l & 0xF000000000000000L) >> 60);
idx = idx < 0 ? idx + 16 : idx;
sbRst.append(HEX[idx]);
idx = (int) ((l & 0x0F00000000000000L) >> 56);
idx = idx < 0 ? idx + 16 : idx;
sbRst.append(HEX[idx]);
idx = (int) ((l & 0x00F0000000000000L) >> 52);
idx = idx < 0 ? idx + 16 : idx;
sbRst.append(HEX[idx]);
idx = (int) ((l & 0x000F000000000000L) >> 48);
idx = idx < 0 ? idx + 16 : idx;
sbRst.append(HEX[idx]);
idx = (int) ((l & 0x0000F00000000000L) >> 44);
idx = idx < 0 ? idx + 16 : idx;
sbRst.append(HEX[idx]);
idx = (int) ((l & 0x00000F0000000000L) >> 40);
idx = idx < 0 ? idx + 16 : idx;
sbRst.append(HEX[idx]);
idx = (int) ((l & 0x000000F000000000L) >> 36);
idx = idx < 0 ? idx + 16 : idx;
sbRst.append(HEX[idx]);
idx = (int) ((l & 0x0000000F00000000L) >> 32);
idx = idx < 0 ? idx + 16 : idx;
sbRst.append(HEX[idx]);
idx = (int) ((l & 0x00000000F0000000L) >> 28);
idx = idx < 0 ? idx + 16 : idx;
sbRst.append(HEX[idx]);
idx = (int) ((l & 0x000000000F000000L) >> 24);
idx = idx < 0 ? idx + 16 : idx;
sbRst.append(HEX[idx]);
idx = (int) ((l & 0x0000000000F00000L) >> 20);
idx = idx < 0 ? idx + 16 : idx;
sbRst.append(HEX[idx]);
idx = (int) ((l & 0x00000000000F0000L) >> 16);
idx = idx < 0 ? idx + 16 : idx;
sbRst.append(HEX[idx]);
idx = (int) ((l & 0x000000000000F000L) >> 12);
idx = idx < 0 ? idx + 16 : idx;
sbRst.append(HEX[idx]);
idx = (int) ((l & 0x0000000000000F00L) >> 8);
idx = idx < 0 ? idx + 16 : idx;
sbRst.append(HEX[idx]);
idx = (int) ((l & 0x00000000000000F0L) >> 4);
idx = idx < 0 ? idx + 16 : idx;
sbRst.append(HEX[idx]);
idx = (int) (l & 0x000000000000000FL);
idx = idx < 0 ? idx + 16 : idx;
sbRst.append(HEX[idx]);
return sbRst.toString();
}
/**
* int 16
*
* @param i
* @return 16
*/
public static String getHexStr(int i)
{
StringBuffer sbRst = new StringBuffer(10);
int idx = (i & 0xF0000000) >> 28;
idx = idx < 0 ? idx + 16 : idx;
sbRst.append(HEX[idx]);
idx = (i & 0x0F000000) >> 24;
idx = idx < 0 ? idx + 16 : idx;
sbRst.append(HEX[idx]);
idx = (i & 0x00F00000) >> 20;
idx = idx < 0 ? idx + 16 : idx;
sbRst.append(HEX[idx]);
idx = (i & 0x000F0000) >> 16;
idx = idx < 0 ? idx + 16 : idx;
sbRst.append(HEX[idx]);
idx = (i & 0x0000F000) >> 12;
idx = idx < 0 ? idx + 16 : idx;
sbRst.append(HEX[idx]);
idx = (i & 0x00000F00) >> 8;
idx = idx < 0 ? idx + 16 : idx;
sbRst.append(HEX[idx]);
idx = (i & 0x000000F0) >> 4;
idx = idx < 0 ? idx + 16 : idx;
sbRst.append(HEX[idx]);
idx = i & 0x0000000F;
idx = idx < 0 ? idx + 16 : idx;
sbRst.append(HEX[idx]);
return sbRst.toString();
}
/**
* Short 16
*
* @param s
* @return 16
*/
public static String getHexStr(short s)
{
StringBuffer sbRst = new StringBuffer(6);
int idx = (s & 0xF000) >> 12;
idx = idx < 0 ? idx + 16 : idx;
sbRst.append(HEX[idx]);
idx = (s & 0x0F00) >> 8;
idx = idx < 0 ? idx + 16 : idx;
sbRst.append(HEX[idx]);
idx = (s & 0x00F0) >> 4;
idx = idx < 0 ? idx + 16 : idx;
sbRst.append(HEX[idx]);
idx = s & 0x000F;
idx = idx < 0 ? idx + 16 : idx;
sbRst.append(HEX[idx]);
return sbRst.toString();
}
/**
* 16
*
* @param pByte
* @return 16
*/
public static String getHexStr(byte pByte)
{
StringBuffer sbRst = new StringBuffer(4);
int idx = (pByte & 0xF0) >> 4;
idx = idx < 0 ? idx + 16 : idx;
sbRst.append(HEX[idx]);
idx = pByte & 0x0F;
idx = idx < 0 ? idx + 16 : idx;
sbRst.append(HEX[pByte & 0x0F]);
return sbRst.toString();
}
}