# Idiom #142 Hexadecimal digits of an integer

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

E.g. 999 -> "3e7"

``const s = x.toString(16)``
``````char s[32];
snprintf(s, sizeof(s), "%x", i);``````
``````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;
}``````
``#include <sstream>``
``````std::ostringstream stream;
stream << std::hex << x;
s = stream.str();``````
``String s = x.ToString("x")``
``import std.conv;``
``s = to!string(x, 16);``
``import std.format;``
``s = format("%x", x);``
``var s = x.toRadixString(16);``
``s = Integer.to_string(x, 16)``
``S = io_lib:fwrite("~.16B",[X]).``
``````write (*,'(Z8.8)') x
``````
``````import "fmt"
import "math/big"``````
``s := fmt.Sprintf("%x", x)``
``import "strconv"``
``s := strconv.FormatInt(x, 16)``
``import Text.Printf (printf)``
``````s :: String
s = printf "%x" 999``````
``String s = String.format("%02x", x);``
``String s = Integer.toHexString(x);``
``s = string.format("%x",x)``
``\$s = dechex(\$x);``
``S := Format("%x", [x]);``
``uses sysutils;``
``s := IntToHex(x);``
``my \$s = sprintf("%x", \$i);``
``s = hex(x)``
``s = x.to_s(16)``
``let s = format!("{:X}", x);``
``val s = x.toString(16)``

programming-idioms.org