简体   繁体   English

如何将 uint32 转换为字符串?

[英]How to convert uint32 to string?

I need to convert an uint32 to string .我需要将uint32转换为string How can I do that?我怎样才能做到这一点? strconv.Itoa doesn't seem to work. strconv.Itoa似乎不起作用。

Long story:很长的故事:
I need to convert an UID received through the imap package to string so that I can set it later as a sequence.我需要将通过 imap 包接收到的 UID 转换为string以便稍后将其设置为序列。 As a side note I'm wondering why such conversions are difficult in Go.作为旁注,我想知道为什么在 Go 中这种转换很困难。 A cast string(t) could have been so much easier.强制转换string(t)本来可以简单得多。

I would do this using strconv.FormatUint :我会使用strconv.FormatUint做到这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.请注意,预期参数是uint64 ,因此您必须先转换uint32 There is no specific FormatUint32 function.没有特定的FormatUint32函数。

I would simply use Sprintf or even just Sprint:我会简单地使用 Sprintf 甚至只是 Sprint:

var n uint32 = 42
str := fmt.Sprint(n)
println(str)

Go is strongly typed. Go 是强类型的。 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 .想想 C,其中 string 是char * ,它是指向以\\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.这就是为什么你需要“主动”转换。

To summarize:总结一下:

strconv.Itoa doesn't seem to work strconv.Itoa 似乎不起作用

strconv.Itoa accepts int , which is signed integer (either 32 or 64 bit), architecture-dependent type (see Numeric types ). strconv.Itoa接受int ,它是有符号整数(32 位或 64 位),体系结构相关类型(请参阅数字类型)。

I need to convert an uint32 to string我需要将 uint32 转换为字符串

  1. Use fmt.Sprint使用fmt.Sprint
  2. Use strconv.FormatUint使用strconv.FormatUint

The better option is strconv.FormatUint because it is faster, has less memory allocations (benchmark examples here or here ).更好的选择strconv.FormatUint因为它更快,内存分配更少(此处此处的基准示例)。

A cast string(t) could have been so much easier.强制转换 string(t) 本来可以简单得多。

Using string does not work as some people expect, see spec :使用string并不像某些人期望的那样工作,请参阅规范

Converting a signed or unsigned integer value to a string type yields a string containing the UTF-8 representation of the integer.将有符号或无符号整数值转换为字符串类型会生成一个包含整数的 UTF-8 表示的字符串。 Values outside the range of valid Unicode code points are converted to "\�".超出有效 Unicode 代码点范围的值将转换为“\�”。

This function is going to be removed from Go2, see Rob Pike's proposal该功能将从 Go2 中移除,参见 Rob Pike 的提议

For a more robust solution, you can use text/template :要获得更强大的解决方案,您可以使用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 https://pkg.go.dev/text/template

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM