Saturday, May 4, 2024
HomeGolangConvert int (int8, int16, int32, int64) to a string in Go (Golang)

Convert int (int8, int16, int32, int64) to a string in Go (Golang)



Use strconv.FormatInt operate to transform an integer variable to a string in Go.

int64 to a decimal string

var quantity int64 = 12
str := strconv.FormatInt(quantity, 10)
fmt.Println(str)

int, int32, int16, int8 to a decimal string

var quantity int = 12 // you should use any integer right here: int32, int16, int8
str := strconv.FormatInt(int64(quantity), 10)
fmt.Println(str)

To transform int to string you too can use strconv.Itoa which is equal to strconv.FormatInt(int64(i), 10).

quantity := 12
str := strconv.Itoa(quantity)
fmt.Println(str)

int64 to a hexadecimal string

var quantity int64 = 12 // you should use any integer right here: int, int32, int16, int8
str := strconv.FormatInt(quantity, 16)
fmt.Println(str)

int64 to an octal string

var quantity int64 = 12 // int, int32, int16, int8
str := strconv.FormatInt(quantity, 8)
fmt.Println(str)

int64 to a binary string

var quantity int64 = 12 // int, int32, int16, int8
str := strconv.FormatInt(quantity, 2)
fmt.Println(str)
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments