如何在C++中将整数转换为十六进制字符串?
我可以找到一些方法来实现它,但它们似乎主要针对C。在C ++中似乎没有本地方法。这是一个非常简单的问题;我有一个int
,我想将其转换为十六进制字符串以便以后打印。
答案 0 :(得分:185)
使用<iomanip>
的{{3}}。如果您打印,只需将其发送到std::cout
,如果没有,则使用std::hex
std::stringstream stream;
stream << std::hex << your_int;
std::string result( stream.str() );
如果您愿意,可以将第一个<<
添加到<< "0x"
或其他任何内容。
其他感兴趣的差异是std::oct
(八进制)和std::dec
(回到十进制)。
您可能遇到的一个问题是,这会产生表示它所需的确切数字位数。您可以使用setfill
和setw
来解决问题:
stream << std::setfill ('0') << std::setw(sizeof(your_type)*2)
<< std::hex << your_int;
最后,我建议这样一个功能:
template< typename T >
std::string int_to_hex( T i )
{
std::stringstream stream;
stream << "0x"
<< std::setfill ('0') << std::setw(sizeof(T)*2)
<< std::hex << i;
return stream.str();
}
答案 1 :(得分:30)
为了使它更轻更快,我建议使用直接填充字符串。
template <typename I> std::string n2hexstr(I w, size_t hex_len = sizeof(I)<<1) {
static const char* digits = "0123456789ABCDEF";
std::string rc(hex_len,'0');
for (size_t i=0, j=(hex_len-1)*4 ; i<hex_len; ++i,j-=4)
rc[i] = digits[(w>>j) & 0x0f];
return rc;
}
答案 2 :(得分:21)
使用std::stringstream
将整数转换为字符串及其特殊操纵符来设置基数。例如:
std::stringstream sstream;
sstream << std::hex << my_integer;
std::string result = sstream.str();
答案 3 :(得分:13)
只需将其打印为十六进制数字:
int i = /* ... */;
std::cout << std::hex << i;
答案 4 :(得分:8)
您可以尝试以下操作。它正在工作......
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;
template <class T>
string to_string(T t, ios_base & (*f)(ios_base&))
{
ostringstream oss;
oss << f << t;
return oss.str();
}
int main ()
{
cout<<to_string<long>(123456, hex)<<endl;
system("PAUSE");
return 0;
}
答案 5 :(得分:4)
感谢林肯在下面的评论,我改变了这个答案。
以下答案在编译时正确处理8位整数。然而,它需要C ++ 17。如果您没有C ++ 17,则必须执行其他操作(例如,提供此函数的重载,一个用于uint8_t,一个用于int8_t,或者使用“if constexpr”之外的其他内容,可能是enable_if)。
template< typename T >
std::string int_to_hex( T i )
{
// Ensure this function is called with a template parameter that makes sense. Note: static_assert is only available in C++11 and higher.
static_assert(std::is_integral<T>::value, "Template argument 'T' must be a fundamental integer type (e.g. int, short, etc..).");
std::stringstream stream;
stream << "0x" << std::setfill ('0') << std::setw(sizeof(T)*2) << std::hex;
// If T is an 8-bit integer type (e.g. uint8_t or int8_t) it will be
// treated as an ASCII code, giving the wrong result. So we use C++17's
// "if constexpr" to have the compiler decides at compile-time if it's
// converting an 8-bit int or not.
if constexpr (std::is_same_v<std::uint8_t, T>)
{
// Unsigned 8-bit unsigned int type. Cast to int (thanks Lincoln) to
// avoid ASCII code interpretation of the int. The number of hex digits
// in the returned string will still be two, which is correct for 8 bits,
// because of the 'sizeof(T)' above.
stream << static_cast<int>(i);
}
else if (std::is_same_v<std::int8_t, T>)
{
// For 8-bit signed int, same as above, except we must first cast to unsigned
// int, because values above 127d (0x7f) in the int will cause further issues.
// if we cast directly to int.
stream << static_cast<int>(static_cast<uint8_t>(i));
}
else
{
// No cast needed for ints wider than 8 bits.
stream << i;
}
return stream.str();
}
原始答案没有按照我的想法正确处理8位整数:
Kornel Kisielewicz的答案很棒。但是稍微添加有助于捕获使用无意义的模板参数调用此函数的情况(例如float)或者会导致编译器错误(例如用户定义的类型)。
template< typename T >
std::string int_to_hex( T i )
{
// Ensure this function is called with a template parameter that makes sense. Note: static_assert is only available in C++11 and higher.
static_assert(std::is_integral<T>::value, "Template argument 'T' must be a fundamental integer type (e.g. int, short, etc..).");
std::stringstream stream;
stream << "0x"
<< std::setfill ('0') << std::setw(sizeof(T)*2)
<< std::hex << i;
// Optional: replace above line with this to handle 8-bit integers.
// << std::hex << std::to_string(i);
return stream.str();
}
我编辑了这个来添加对std :: to_string的调用,因为传递给std::uint8_t
的8位整数类型(例如std::stringstream
值)被视为char,它不会给你你想要的结果。将这样的整数传递给std::to_string
可以正确处理它们,并且在使用其他更大的整数类型时不会损害它们。当然,在这些情况下你可能会遇到轻微的性能损失,因为std :: to_string调用是不必要的。
注意:我会在对原始答案的评论中添加此内容,但我没有代表发表评论。
答案 6 :(得分:4)
int num = 30;
std::cout << std::hex << num << endl; // This should give you hexa- decimal of 30
答案 7 :(得分:3)
这个问题很古老,但是我很惊讶为什么没有人提到boost::format
:
cout << (boost::format("%x") % 1234).str(); // output is: 4d2
答案 8 :(得分:2)
对于那些发现许多/大多数ios::fmtflags
不能与std::stringstream
合作但又喜欢Kornel回归的模板构思的人,以下作品相对干净:
#include <iomanip>
#include <sstream>
template< typename T >
std::string hexify(T i)
{
std::stringbuf buf;
std::ostream os(&buf);
os << "0x" << std::setfill('0') << std::setw(sizeof(T) * 2)
<< std::hex << i;
return buf.str().c_str();
}
int someNumber = 314159265;
std::string hexified = hexify< int >(someNumber);
答案 9 :(得分:2)
代码供您参考:
#include <iomanip>
#include <sstream>
...
string intToHexString(int intValue) {
string hexStr;
/// integer value to hex-string
std::stringstream sstream;
sstream << "0x"
<< std::setfill ('0') << std::setw(2)
<< std::hex << (int)intValue;
hexStr= sstream.str();
sstream.clear(); //clears out the stream-string
return hexStr;
}
答案 10 :(得分:2)
看看我的解决方案, [1] ,我从我的项目中逐字复制,因此包含了德语API文档。我的目标是在我的实际需求中结合灵活性和安全性: [2]
0x
前缀:来电者可以决定long long
#include <string>
#include <sstream>
#include <iomanip>
/// Vertextet einen Ganzzahlwert val im Hexadezimalformat.
/// Auf die Minimal-Breite width wird mit führenden Nullen aufgefüllt;
/// falls nicht angegeben, wird diese Breite aus dem Typ des Arguments
/// abgeleitet. Funktion geeignet von char bis long long.
/// Zeiger, Fließkommazahlen u.ä. werden nicht unterstützt, ihre
/// Übergabe führt zu einem (beabsichtigten!) Compilerfehler.
/// Grundlagen aus: http://stackoverflow.com/a/5100745/2932052
template <typename T>
inline std::string int_to_hex(T val, size_t width=sizeof(T)*2)
{
std::stringstream ss;
ss << std::setfill('0') << std::setw(width) << std::hex << (val|0);
return ss.str();
}
[1] 基于Kornel Kisielewicz的答案 [2] 翻译成CppTest的语言,它的内容如下:
TEST_ASSERT(int_to_hex(char(0x12)) == "12");
TEST_ASSERT(int_to_hex(short(0x1234)) == "1234");
TEST_ASSERT(int_to_hex(long(0x12345678)) == "12345678");
TEST_ASSERT(int_to_hex((long long)(0x123456789abcdef0)) == "123456789abcdef0");
TEST_ASSERT(int_to_hex(0x123, 1) == "123");
TEST_ASSERT(int_to_hex(0x123, 8) == "00000123");
// with deduction test as suggested by Lightness Races in Orbit:
TEST_ASSERT(int_to_hex(short(0x12)) == "0012");
答案 11 :(得分:1)
我做:
int hex = 10;
std::string hexstring = stringFormat("%X", hex);
答案 12 :(得分:1)
我的解决方案。仅允许整数类型。
更新。您可以在第二个参数中设置可选的前缀0x。
definition.h
#include <iomanip>
#include <sstream>
template <class T, class T2 = typename std::enable_if<std::is_integral<T>::value>::type>
static std::string ToHex(const T & data, bool addPrefix = true);
template<class T, class>
inline std::string Convert::ToHex(const T & data, bool addPrefix)
{
std::stringstream sstream;
sstream << std::hex;
std::string ret;
if (typeid(T) == typeid(char) || typeid(T) == typeid(unsigned char) || sizeof(T)==1)
{
sstream << static_cast<int>(data);
ret = sstream.str();
if (ret.length() > 2)
{
ret = ret.substr(ret.length() - 2, 2);
}
}
else
{
sstream << data;
ret = sstream.str();
}
return (addPrefix ? u8"0x" : u8"") + ret;
}
main.cpp
#include <definition.h>
int main()
{
std::cout << ToHex<unsigned char>(254) << std::endl;
std::cout << ToHex<char>(-2) << std::endl;
std::cout << ToHex<int>(-2) << std::endl;
std::cout << ToHex<long long>(-2) << std::endl;
std::cout<< std::endl;
std::cout << ToHex<unsigned char>(254, false) << std::endl;
std::cout << ToHex<char>(-2, false) << std::endl;
std::cout << ToHex<int>(-2, false) << std::endl;
std::cout << ToHex<long long>(-2, false) << std::endl;
return 0;
}
结果:
0xfe
0xfe
0xfffffffe
0xfffffffffffffffe
fe
FE
fffffffe
ffpffffffffffff
答案 13 :(得分:1)
我想添加一个答案,以享受C ++语言的美丽。它的适应性可以在高低水平下工作。编程愉快。
public:template <class T,class U> U* Int2Hex(T lnumber, U* buffer)
{
const char* ref = "0123456789ABCDEF";
T hNibbles = (lnumber >> 4);
unsigned char* b_lNibbles = (unsigned char*)&lnumber;
unsigned char* b_hNibbles = (unsigned char*)&hNibbles;
U* pointer = buffer + (sizeof(lnumber) << 1);
*pointer = 0;
do {
*--pointer = ref[(*b_lNibbles++) & 0xF];
*--pointer = ref[(*b_hNibbles++) & 0xF];
} while (pointer > buffer);
return buffer;
}
示例:
char buffer[100] = { 0 };
Int2Hex(305419896ULL, buffer);//returns "0000000012345678"
Int2Hex(305419896UL, buffer);//returns "12345678"
Int2Hex((short)65533, buffer);//returns "FFFD"
Int2Hex((char)18, buffer);//returns "12"
wchar_t buffer[100] = { 0 };
Int2Hex(305419896ULL, buffer);//returns L"0000000012345678"
Int2Hex(305419896UL, buffer);//returns L"12345678"
Int2Hex((short)65533, buffer);//returns L"FFFD"
Int2Hex((char)18, buffer);//returns L"12"
答案 14 :(得分:1)
从 C++20 开始,使用 std::format
,您可以:
std::format("{:#x}", your_int); // 0x2a
std::format("{:#010x}", your_int); // 0x0000002a
答案 15 :(得分:0)
#include <iostream>
#include <sstream>
int main()
{
unsigned int i = 4967295; // random number
std::string str1, str2;
unsigned int u1, u2;
std::stringstream ss;
使用空指针:
// INT to HEX
ss << (void*)i; // <- FULL hex address using void pointer
ss >> str1; // giving address value of one given in decimals.
ss.clear(); // <- Clear bits
// HEX to INT
ss << std::hex << str1; // <- Capitals doesn't matter so no need to do extra here
ss >> u1;
ss.clear();
添加0x:
// INT to HEX with 0x
ss << "0x" << (void*)i; // <- Same as above but adding 0x to beginning
ss >> str2;
ss.clear();
// HEX to INT with 0x
ss << std::hex << str2; // <- 0x is also understood so need to do extra here
ss >> u2;
ss.clear();
输出:
std::cout << str1 << std::endl; // 004BCB7F
std::cout << u1 << std::endl; // 4967295
std::cout << std::endl;
std::cout << str2 << std::endl; // 0x004BCB7F
std::cout << u2 << std::endl; // 4967295
return 0;
}
答案 16 :(得分:0)
用于固定数字位数,例如2:
static const char* digits = "0123456789ABCDEF";//dec 2 hex digits positional map
char value_hex[3];//2 digits + terminator
value_hex[0] = digits[(int_value >> 4) & 0x0F]; //move of 4 bit, that is an HEX digit, and take 4 lower. for higher digits use multiple of 4
value_hex[1] = digits[int_value & 0x0F]; //no need to move the lower digit
value_hex[2] = '\0'; //terminator
您还可以编写一个for循环变体来处理可变数字位数
好处:
答案 17 :(得分:0)
char buf[_MAX_U64TOSTR_BASE2_COUNT];
_itoa_s(10, buf, _countof(buf), 16);
printf("%s\n", buf); // a
uint8_t x = 10;
wchar_t buf[_MAX_ITOSTR_BASE16_COUNT];
swprintf_s(buf, L"%02X", x);
答案 18 :(得分:0)
char_to_hex 返回两个字符的字符串
const char HEX_MAP[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
char replace(unsigned char c)
{
return HEX_MAP[c & 0x0f];
}
std::string char_to_hex(unsigned char c)
{
std::string hex;
// First four bytes
char left = (c >> 4);
// Second four bytes
char right = (c & 0x0f);
hex += replace(left);
hex += replace(right);
return hex;
}
答案 19 :(得分:-1)
`
char sw(int num){
char h[16] = {'0','1','2','3','4','5','6','7','8','9','a','b',' c','d','e','f'};
return h[num];
}
String to_hexa(int num) {
int i, j, exp = 0, temp = num;
String hexa;
String begin;
while( temp >= 16 ){
exp++;
temp /= 16;
}
for ( i = 0; i <= exp; ++i){
temp = num;
for ( j = i; j <= exp; ++j){
if ( j == exp ) hexa += String(sw(temp % 16));
else temp /= 16;
}
}
begin = "0x";
for (int i=0; i < 3 - exp; i++){
begin += "0";
}
return begin + hexa;
}
`
答案 20 :(得分:-1)
int var = 20;
cout << &var << endl;
cout << (int)&var << endl;
cout << std::hex << "0x" << (int)&var << endl << std::dec; // output in hex, reset back to dec
0x69fec4(地址)
6946500(十月的地址)
0x69fec4(地址到dec,以十六进制输出)
本能地去了...
int地址=(int)&var;
在其他地方看到了...
无符号长地址= reinterpret_cast(&var);
评论告诉我这是正确的...
int地址=(int)&var;
speaking of well covered 轻,你在哪里?他们得到了很多喜欢!