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'};
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容