Decimal to Hexadecimal

Modulus vs Bit manipulation &

When converting a decimal number to hexadecimal, I always use modulus. There is another way of doing this.

  • Mod operation
int digit = num % 16
  • Bit operation
int digit = num & 15

查表法

将所有元素临时存起来,建立对应关系,每一次将&15后的值作为索引去查表,就可找到对应的元素。

char[] chs = {'0','1','2','3',
              '4','5','6','7',
              '8','9','A','B',
              'C','D','E','F'};

你可能感兴趣的:(Decimal to Hexadecimal)