Given an integer, write an algorithm to convert it to hexadecimal(16进制的数). 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
0
s. 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"
把十进制数转化为十六进制数。正数很简单,负数怎么做?
负数转换成二进制分为3步:
1、首先将负数转换为对应的原码
-3的原码为(也就是+3转换成二进制后的字符串):
0000 0000 0000 0000 0000 0000 0000 0011
2、再将原码的每一位做取反操作得到反码。 取反操作:0变为1,1变为0; 取反后的结果即为:
1111 1111 1111 1111 1111 1111 1111 1100
3、将反码+1得到补码
1111 1111 1111 1111 1111 1111 1111 1101
负数转换成十六进制,只需在补码(二进制)的基础上,4位合成一位计算:
将-3的二进制从右至左每4位合并为一个单元,即:
1111 1111 1111 1111 1111 1111 1111 1101
计算后为: FFFFFFFD
其实正数也可以这么做,如26为:0000 0000 0000 0000 0000 0000 0001 1010,就是1A(删去了前导0)。
因此,无论是正数还是负数,都可以先转化为二进制,再 每4位组合,来计算十六进制。
package leetcode;
import java.util.HashMap;
public class Convert_a_Number_to_Hexadecimal_405 {
public String toHex(int num) {
if(num==0){
return "0";
}
HashMap<Integer, Character> map=new HashMap<Integer, Character>();
map.put(0,'0');
map.put(1,'1');map.put(2,'2');map.put(3,'3');
map.put(4,'4');map.put(5,'5');map.put(6,'6');
map.put(7,'7');map.put(8,'8');map.put(9,'9');
map.put(10,'a');map.put(11,'b');map.put(12,'c');
map.put(13,'d');map.put(14,'e');map.put(15,'f');
int[] bits=new int[32];
long absNum=Math.abs((long)num);
int pointer=31;
while(absNum>0){
if(absNum%2==1){
bits[pointer]=1;
}
absNum=absNum/2;
pointer--;
}
if(num<0){
reverseAddOne(bits);
}
String result="";
boolean isLeadingZero=true;
for(int i=0;i<8;i++){
int one=bits[i*4];
int two=bits[i*4+1];
int three=bits[i*4+2];
int four=bits[i*4+3];
if(isLeadingZero&&one==0&&two==0&&three==0&&four==0){
continue;//前导0 可以忽略
}
isLeadingZero=false;//之后的0就不是前导0了
int thisNumber=one*8+two*4+three*2+four;
result=result+map.get(thisNumber);
}
return result;
}
public void reverseAddOne(int[] bits){//取反加一
for(int i=0;i<32;i++){
bits[i]=1-bits[i];//1-0=1,1-1=0
}
int pointer=31;
while(pointer>=0){
if(bits[pointer]==0){
bits[pointer]=1;
break;
}
else{
bits[pointer]=0;
pointer--;
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Convert_a_Number_to_Hexadecimal_405 c=new Convert_a_Number_to_Hexadecimal_405();
System.out.println(c.toHex(16));
System.out.println(c.toHex(-2147483648));
}
}
大神的方法则特别简洁:
/*
Basic idea: each time we take a look at the last four digits of
binary verion of the input, and maps that to a hex char
shift the input to the right by 4 bits, do it again
until input becomes 0.
*/
public class Solution {
char[] map = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
public String toHex(int num) {
if(num == 0) return "0";
String result = "";
while(num != 0){
result = map[(num & 15)] + result;
num = (num >>> 4);
}
return result;
}
}
Java中的位运算符:
>>表示右移,如果该数为正,则高位补0,若为负数,则高位补1;
>>>表示无符号右移,也叫逻辑右移,即若该数为正,则高位补0,而若该数为负数,则右移后高位同样补0。