How to convert uint8 to string
Asked Answered
G

5

25

I want to convert uint8 to string but can't figure out how.

package main

import "fmt"
import "strconv"

func main() {
    str := "Hello"
    fmt.Println(str[1])  // 101

    fmt.Println(strconv.Itoa(str[1]))
}

Example

This gives me prog.go:11: cannot use str[1] (type uint8) as type int in function argument [process exited with non-zero status]

Any idea?

Godthaab answered 7/10, 2013 at 11:9 Comment(2)
Check the first part of this answer if you are looking for information about slices of uint8 ([]uint8): https://mcmap.net/q/56917/-how-to-convert-int8-to-stringJakejakes
string(u) or fmt.Sprintf("%s", u) to set []uint8 to a sting. See https://mcmap.net/q/56302/-how-to-convert-uint8-to-string. []uint8("abc") to set a string to []uint8. See https://mcmap.net/q/55268/-how-can-i-convert-string-to-integer-in-golang.Jeaninejeanlouis
J
32

Simply convert it :

fmt.Println(strconv.Itoa(int(str[1])))
Jeth answered 7/10, 2013 at 11:11 Comment(0)
B
12

There is a difference between converting it or casting it, consider:

var s uint8 = 10
fmt.Print(string(s))
fmt.Print(strconv.Itoa(int(s)))

The string cast prints '\n' (newline), the string conversion prints "10". The difference becomes clear once you regard the []byte conversion of both variants:

[]byte(string(s)) == [10] // the single character represented by 10
[]byte(strconv.Itoa(int(s))) == [49, 48] // character encoding for '1' and '0'
see this code in play.golang.org
Bobbi answered 19/8, 2015 at 8:17 Comment(2)
strconv.Itoa(int(s))) - is exactly what I was looking for to convert numeric representation of byte value into stringHelbonia
There is a difference between conversion and casting. But in Go, casting is not possible. Only conversion is possible in Go.Snappish
E
7

You can do it even simpler by using casting, this worked for me:

var c uint8
c = 't'
fmt.Printf(string(c))
Eyesight answered 3/2, 2014 at 5:24 Comment(2)
Accepted answer is correct. As @Bobbi explains below, this method does not do what some might expect.Jakejakes
casting using string(c) is a hack in Go which might be removed soon. See Rob Pikes response to the question on RedditMirtamirth
J
0

There are no automatic conversions of basic types in Go expressions. See https://talks.golang.org/2012/goforc.slide#18. A byte (an alias of uint8) or []byte ([]uint8) has to be set to a bool, number or string.

package main

import (
    . "fmt"
)

func main() {
    b := []byte{'G', 'o'}
    c := []interface{}{b[0], float64(b[0]), int(b[0]), rune(b[0]), string(b[0]), Sprintf("%s", b), b[0] != 0}
    checkType(c)
}

func checkType(s []interface{}) {
    for k, _ := range s {
        // uint8 71, float64 71, int 71, int32 71, string G, string Go, bool true
        Printf("%T %v\n", s[k], s[k])
    }
}

Sprintf("%s", b) can be used to convert []byte{'G', 'o' } to the string "Go". You can convert any int type to a string with Sprintf. See https://mcmap.net/q/56918/-string-to-number-conversion-in-golang.

But Sprintf uses reflection. See the comment in https://mcmap.net/q/53864/-how-to-convert-an-int-value-to-string-in-go. Using Itoa (Integer to ASCII) is faster. See @DenysSéguret and https://mcmap.net/q/53864/-how-to-convert-an-int-value-to-string-in-go. Quotes edited.

Jeaninejeanlouis answered 4/7, 2020 at 5:24 Comment(1)
K
-1

use %c

    str := "Hello"
    fmt.Println(str[1]) // 101
    fmt.Printf("%c\n", str[1])
Kenyon answered 17/7, 2020 at 3:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.