英文:
How to convert uint32 to string?
问题
我需要将一个uint32
转换为string
。我该如何做?strconv.Itoa
似乎不起作用。
长话短说:
我需要将通过imap包接收到的UID转换为string
,以便稍后将其设置为序列。顺便说一下,我想知道为什么在Go语言中这样的转换很困难。一个简单的强制类型转换string(t)
本来可以更容易些。
英文:
I need to convert an uint32
to string
. How can I do that? strconv.Itoa
doesn't seem to work.
Long story:<br>
I need to convert an UID received through the imap package to string
so that I can set it later as a sequence. As a side note I'm wondering why such conversions are difficult in Go. A cast string(t)
could have been so much easier.
答案1
得分: 87
我会使用strconv.FormatUint
来完成这个任务:
import "strconv"
var u uint32 = 17
var s = strconv.FormatUint(uint64(u), 10)
// "17"
请注意,FormatUint
函数的参数类型是uint64
,所以你需要先将uint32
进行类型转换。没有专门的FormatUint32
函数。
英文:
I would do this using strconv.FormatUint
:
import "strconv"
var u uint32 = 17
var s = strconv.FormatUint(uint64(u), 10)
// "17"
Note that the expected parameter is uint64
, so you have to cast your uint32
first. There is no specific FormatUint32
function.
答案2
得分: 67
我会简单地使用Sprintf或者只用Sprint:
var n uint32 = 42
str := fmt.Sprint(n)
println(str)
Go是强类型语言。直接将一个数字转换为字符串是没有意义的。想想在C语言中,字符串是char *
类型,它是指向字符串第一个字母的指针,并以\0
结尾。将一个数字转换为字符串会导致第一个字母指向数字的地址,这是没有意义的。这就是为什么你需要"主动"进行转换的原因。
英文:
I would simply use Sprintf or even just Sprint:
var n uint32 = 42
str := fmt.Sprint(n)
println(str)
Go is strongly typed. Casting a number directly to a string would not make sense. Think about C where string are char *
which is a pointer to the first letter of the string terminated by \0
. Casting a number to a string would result in having the first letter pointer to the address of the number, which does not make sense. This is why you need to "actively" convert.
答案3
得分: 6
总结一下:
> strconv.Itoa似乎不起作用
strconv.Itoa
接受int
类型,它是有符号整数(32位或64位),与架构相关的类型(参见数字类型)。
> 我需要将uint32转换为字符串
更好的选择是strconv.FormatUint
,因为它更快,内存分配更少(参见这里或这里的基准示例)。
>强制转换为字符串(t)可能会更容易。
使用string
并不像一些人期望的那样起作用,请参见规范:
>将有符号或无符号整数值转换为字符串类型会产生一个包含整数的UTF-8表示的字符串。超出有效Unicode代码点范围的值会转换为“\uFFFD”。
这个函数将在Go2中被移除,参见Rob Pike的提案
英文:
To summarize:
> strconv.Itoa doesn't seem to work
strconv.Itoa
accepts int
, which is signed integer (either 32 or 64 bit), architecture-dependent type (see Numeric types).
> I need to convert an uint32 to string
- Use
fmt.Sprint
- Use
strconv.FormatUint
The better option is strconv.FormatUint
because it is faster, has less memory allocations (benchmark examples here or here).
>A cast string(t) could have been so much easier.
Using string
does not work as some people expect, see spec:
>Converting a signed or unsigned integer value to a string type yields a string containing the UTF-8 representation of the integer. Values outside the range of valid Unicode code points are converted to "\uFFFD".
This function is going to be removed from Go2, see Rob Pike's proposal
答案4
得分: 1
为了更稳健的解决方案,你可以使用text/template
:
package main
import (
"text/template"
"strings"
)
func format(s string, v interface{}) string {
t, b := new(template.Template), new(strings.Builder)
template.Must(t.Parse(s)).Execute(b, v)
return b.String()
}
func main() {
imap := struct{UID uint32}{999}
s := format("{{.UID}}", imap)
println(s == "999")
}
https://pkg.go.dev/text/template
英文:
For a more robust solution, you can use text/template
:
package main
import (
"text/template"
"strings"
)
func format(s string, v interface{}) string {
t, b := new(template.Template), new(strings.Builder)
template.Must(t.Parse(s)).Execute(b, v)
return b.String()
}
func main() {
imap := struct{UID uint32}{999}
s := format("{{.UID}}", imap)
println(s == "999")
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论