Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!
  • Go

Idiom #142 Hexadecimal digits of an integer

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

E.g. 999 -> "3e7"

import "fmt"
import "math/big"
s := fmt.Sprintf("%x", x)

x has type *big.Int.

This works because *big.Int implements the fmt.Formatter interface.

%x is the "verb" for base 16. Not to be confused with the variable name x.
import "strconv"
s := strconv.FormatInt(x, 16)
char s[32];
snprintf(s, sizeof(s), "%x", i);

New implementation...