Logo

Programming-Idioms

History of Idiom 142 > diff from v21 to v22

Edit summary for version 22 by Anonyme:
New Cpp implementation by user [Anonyme]

Version 21

2019-09-26, 14:29:43

Version 22

2019-09-26, 16:38:25

Idiom #142 Hexadecimal digits of an integer

Assign to string s the hexadecimal representation (base 16) of integer x.

E.g. 999 -> "3e7"

Idiom #142 Hexadecimal digits of an integer

Assign to string s the hexadecimal representation (base 16) of integer x.

E.g. 999 -> "3e7"

Extra Keywords
hex hexa radix
Extra Keywords
hex hexa radix
Code
template <typename I>
std::string n2hexstr(I w, size_t hex_len = sizeof(I)<<1) {
    static const char* digits = "0123456789ABCDEF";
	std::string str(hex_len, '-');
    for (size_t i=0, j=(hex_len-1)*4 ; i<hex_len; ++i,j-=4)
       str[i] = digits[(w>>j) & 0x0f];
	return str;
}