Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Integer to hex string in C++

Tags:

c++

int

decimal

hex

How do I convert an integer to a hex string in C++?

I can find some ways to do it, but they mostly seem targeted towards C. It doesn't seem there's a native way to do it in C++. It is a pretty simple problem though; I've got an int which I'd like to convert to a hex string for later printing.

like image 996
kryptobs2000 Avatar asked Feb 24 '11 05:02

kryptobs2000


People also ask

How to convert int to hex in c++?

Convert an integer to a hex string in C++ In C / C++ there is a format specifier %X. It prints the value of some variable into hexadecimal form. We have used this format specifier to convert number into a string by using sprintf() function.

What is hex string in C?

Keep in mind that a string in C is an array of char values. And a char is an unsigned 8-bit quantity. (Just as an int is a signed 16-bit quantity.) The maximum value that can be placed in an unsigned 8-bit field is 255 decimal which is 0xFF hex or 0377 octal or 11111111 binary.

How can I convert a hex string to an integer value?

To convert a hexadecimal string to a numberUse the ToInt32(String, Int32) method to convert the number expressed in base-16 to an integer. The first argument of the ToInt32(String, Int32) method is the string to convert. The second argument describes what base the number is expressed in; hexadecimal is base 16.

Does atoi convert hex?

The atoi() and atol() functions convert a character string containing decimal integer constants, but the strtol() and strtoul() functions can convert a character string containing a integer constant in octal, decimal, hexadecimal, or a base specified by the base parameter.


2 Answers

Use <iomanip>'s std::hex. If you print, just send it to std::cout, if not, then use std::stringstream

std::stringstream stream; stream << std::hex << your_int; std::string result( stream.str() ); 

You can prepend the first << with << "0x" or whatever you like if you wish.

Other manips of interest are std::oct (octal) and std::dec (back to decimal).

One problem you may encounter is the fact that this produces the exact amount of digits needed to represent it. You may use setfill and setw this to circumvent the problem:

stream << std::setfill ('0') << std::setw(sizeof(your_type)*2)         << std::hex << your_int; 

So finally, I'd suggest such a function:

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(); } 
like image 83
Kornel Kisielewicz Avatar answered Oct 06 '22 14:10

Kornel Kisielewicz


To make it lighter and faster I suggest to use direct filling of a string.

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; } 
like image 31
AndreyS Scherbakov Avatar answered Oct 06 '22 14:10

AndreyS Scherbakov