java 10进制转16进制

2024-11-27 18:01:51
推荐回答(2个)
回答1:

移位的办法你不知道怎么写是么?

String toHexString (int num)
{
char [] hexStr = new char [8] ;
for (int i = 0 ; i < 4 ; i ++)
{
// 取低 4 bits
hexStr [7 - i] = getHexChar (num & 0x0F) ;
// 右移4 Bits ;
num = num >> 4 ;
}

return new String (hexStr) ;
}

char getHexChar1 (int num)
{
if (num < 0 || num >= 16)
num = num % 16 ;

if (num < 10)
return '0' + num ;
else
return 'A' + num - 10 ;
}

static char [] HEX_DIGITS_LIST = new char [] {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'} ;
char getHexChar2 (int num)
{
if (num < 0 || num >= 16)
num = num % 16 ;

return HEX_DIGITS_LIST [num] ;
}

char getHexChar3 (int num)
{
if (num < 0 || num >= 16)
num = num % 16 ;

return Character.forDigit (num) ;
}

回答2:

太了