Convert int8 and int16 into String?

Hey,

Can someone please tell me that how can I convert int8 or int16 to a string?

Does the strconv package help you?

I did look into that but strconv is for the int64, and I need for int8 and int16

You can losslessly convert int8 and int16 to int64.

Hello,

When you convert int8 or int16 to string, its size becomes bigger then the size of an int64. Converting to int64 before converting to string is ok. Please check following code.

package main

import (
    "fmt"
    "unsafe"
)

func main() {
    var i1 int8 = 1
    var i2 int64 = int64(i1)
    var s1 string = "1"

    fmt.Printf("Size of i1 (int8)  : %v byte\n", unsafe.Sizeof(i1))
    fmt.Printf("Size of i2 (int64) : %v bytes\n", unsafe.Sizeof(i2))
    fmt.Printf("Size of s1 (string): %v bytes\n", unsafe.Sizeof(s1))
}

Output:

Size of i1 (int8)  : 1 byte
Size of i2 (int64) : 8 bytes
Size of s1 (string): 16 bytes
1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.