c_cpp 405.将Number转换为Hexadecimal.cpp
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp 405.将Number转换为Hexadecimal.cpp相关的知识,希望对你有一定的参考价值。
public class Solution {
public String toHex(int num) {
StringBuilder res = new StringBuilder();
char[] dict = new char[] {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
while (num != 0) {
//for (int i = 0; i < 8; i++) {
int temp = num & 15;
res.append(dict[temp]);
num = num >>> 4;
}
return res.length() == 0 ? "0" : res.reverse().toString();
}
}
const string HEX = "0123456789abcdef";
class Solution {
public:
string toHex(int num) {
if (num == 0) return "0";
string result;
int count = 0;
while (num && count++ < 8) {
result = HEX[(num & 0xf)] + result;
num >>= 4;
}
return result;
}
};
以上是关于c_cpp 405.将Number转换为Hexadecimal.cpp的主要内容,如果未能解决你的问题,请参考以下文章
c_cpp 405.将Number转换为Hexadecimal.cpp
c_cpp 405.将Number转换为Hexadecimal.cpp
c_cpp 405.将Number转换为Hexadecimal.cpp
c_cpp 405.将Number转换为Hexadecimal.cpp
405 Convert a Number to Hexadecimal 数字转换为十六进制数
c_cpp 405.cpp