LeetCode 405. Convert a Number to Hexadecimal

Description

Given an integer, write an algorithm to convert it to hexadecimal. For negative integer, two’s complement method is used.

Note:

All letters in hexadecimal (a-f) must be in lowercase.
The hexadecimal string must not contain extra leading 0s. If the number is zero, it is represented by a single zero character ‘0’; otherwise, the first character in the hexadecimal string will not be the zero character.
The given number is guaranteed to fit within the range of a 32-bit signed integer.
You must not use any method provided by the library which converts/formats the number to hex directly.
Example 1:

```Input:
26

Output:
"1a"
```

Example 2:

```Input:
-1

Output:
"ffffffff"
```

描述

```输入:
26

"1a"
```

```输入:
-1

"ffffffff"
```

思路

• 将负数转换为补码：我们知道对于一个负数 -a（a>0）,其补码为 a 的二进制表示按位取反，然后加一；为什要这么做？因为要使得负数的二进制表示与这个负数绝对值的二进制表示加和为 0；比如 0b1，将最高位置为 1，按位取反后（假设只有 8 位）为 0b11111110，此时把这辆个数加起来为 0b1111111，此时这个数再加上 1，就可以变成 0，于是负数的补码是，此数绝对值的二进制的反码 + 1;
• 为了求的一个数 num （num < 0）的补码，我们记要求的补码为 x，则有 x + （-num） = 0，于是 x = 0 – num，在 32 位表示中，0 为 0xffffffff + 1，
• 将负数转换成为补码后，对 16 取模，记下余数；如此循环，直到商为 0；
```# -*- coding: utf-8 -*-
# @Author:             何睿
# @Create Date:        2019-09-14 10:47:02
# @Last Modified by:   何睿
# @Last Modified time: 2019-09-14 11:07:11

class Solution:
def toHex(self, num: int) -> str:
if num < 0:
num = 0xffffffff + 1 + num

transfer = "0123456789abcdef"
res = []
while num:
res.append(transfer[num % 16])
num >>= 4

return ''.join(reversed(res)) or '0'

```

©本文是原创文章，欢迎转载，转载需保留 文章来源 ，作者信息和本声明.

Posted in LeetCode and tagged , .

何 睿

This site uses Akismet to reduce spam. Learn how your comment data is processed.