Convert a Number to Hexadecimal

Problem description

https://leetcode-cn.com/problems/convert-a-number-to-hexadecimal/

Solution

转换十六进制有个技巧,四位一转,每次取出后四位abcd出来与 0xf 与下,得到的结果必然是abcd 代表的十进制数 i,再根据 i 从16 进制索引表中取出对应的 16 进制字符。小细节是,每次需要将上次的结果放到后面去

Code

1
2
3
4
5
6
7
8
9
10
11
12
class Solution {
fun toHex(num: Int): String {
var n = num
var res = ""
val string = "0123456789abcdef"
while (n != 0 && res.length < 8) {
res = string[n and 0xf] + res
n = n shr 4
}
return res
}
}
文章目录
  1. 1. Problem description
    1. 1.1. https://leetcode-cn.com/problems/convert-a-number-to-hexadecimal/
  2. 2. Solution
  3. 3. Code
|