How to convert uint8 to string

huangapple go评论87阅读模式
英文:

How to convert uint8 to string

问题

我想将uint8转换为字符串,但是无法弄清楚如何做。

package main

import "fmt"
import "strconv"

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

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

示例

这给出了prog.go:11: cannot use str[1] (type uint8) as type int in function argument [process exited with non-zero status]

有什么想法吗?

英文:

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?

答案1

得分: 31

只需将其转换为:

fmt.Println(strconv.Itoa(int(str[1])))
英文:

Simply convert it :

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

答案2

得分: 11

在转换或强制转换之间存在差异,请考虑以下代码:

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

字符串强制转换打印出'\n'(换行符),而字符串转换打印出"10"。一旦你考虑到两种变体的[]byte转换,差异就变得清楚了:

[]byte(string(s)) == [10] // 由10表示的单个字符
[]byte(strconv.Itoa(int(s))) == [49, 48] // '1'和'0'的字符编码

你可以在play.golang.org上查看这段代码:点击这里

英文:

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'

<iframe src="http://play.golang.org/p/kek9D7ol22" frameborder="0" style="width: 100%; height: 100%"><a href="http://play.golang.org/p/kek9D7ol22">see this code in play.golang.org</a></iframe>

答案3

得分: 7

你可以通过使用类型转换来更简单地完成,这对我有效:

var c uint8
c = 't'
fmt.Printf(string(c))
英文:

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

var c uint8
c = &#39;t&#39;
fmt.Printf(string(c))

答案4

得分: 0

在Go表达式中,基本类型没有自动转换。请参阅https://talks.golang.org/2012/goforc.slide#18。byteuint8的别名)或[]byte[]uint8)必须设置为bool、数字或字符串。

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)可以将[]byte{'G', 'o'}转换为字符串"Go"。您可以使用Sprintf将任何int类型转换为字符串。请参阅https://stackoverflow.com/a/41074199/12817546。

但是,Sprintf使用了反射。请参阅https://stackoverflow.com/a/22626531/12817546中的注释。使用Itoa(整数转ASCII)更快。请参阅@DenysSéguret和https://stackoverflow.com/a/38077508/12817546。引号已编辑。

英文:

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 (
	. &quot;fmt&quot;
)

func main() {
	b := []byte{&#39;G&#39;, &#39;o&#39;}
	c := []interface{}{b[0], float64(b[0]), int(b[0]), rune(b[0]), string(b[0]), Sprintf(&quot;%s&quot;, 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(&quot;%T %v\n&quot;, s[k], s[k])
	}
}

Sprintf(&quot;%s&quot;, b) can be used to convert []byte{&#39;G&#39;, &#39;o&#39; } to the string "Go". You can convert any int type to a string with Sprintf. See https://stackoverflow.com/a/41074199/12817546.

But Sprintf uses reflection. See the comment in https://stackoverflow.com/a/22626531/12817546. Using Itoa (Integer to ASCII) is faster. See @DenysSéguret and https://stackoverflow.com/a/38077508/12817546. Quotes edited.

答案5

得分: -1

使用%c

	str := "Hello"
	fmt.Println(str[1]) // 101
	fmt.Printf("%c\n", str[1])

使用%c可以将整数值解释为对应的Unicode字符。在上面的示例中,str[1]的值为101,对应的Unicode字符是'e'。因此,fmt.Printf("%c\n", str[1])会输出'e'。

英文:

use %c

	str := &quot;Hello&quot;
	fmt.Println(str[1]) // 101
	fmt.Printf(&quot;%c\n&quot;, str[1])

huangapple
  • 本文由 发表于 2013年10月7日 19:09:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/19223277.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定