写入文件int转换为字符串(Writing to file int converted to string)

编程入门 行业动态 更新时间:2024-10-15 22:26:34
写入文件int转换为字符串(Writing to file int converted to string)

我只想简单地写一个从int转换的字符串。 但f.WriteString而不是数字从ASCII代码表中写入符号。

我预计“noReport = 12320 nr3h = 105 nr2h = 162 nr1h = 38 ok = 16899”

但反而得到了“noReport =〠nr3h= i nr2h =¢nr1h =&ok =䈃”

I just want simply write to file a string converted from int. But f.WriteString instead of number write a symbols from ASCII code table.

I expected "noReport = 12320 nr3h = 105 nr2h = 162 nr1h = 38 ok = 16899"

But instead got "noReport = 〠 nr3h = i nr2h = ¢ nr1h = & ok = 䈃"

最满意答案

https://play.golang.org/p/QAXJ4aJBy3

fmt.Printf("os.StdOut is %T\n", os.Stdout) os.Stdout.WriteString("noReport = 12320 nr3h = 105 nr2h = 162 nr1h = 38 ok = 16899 \n") os.Stdout.WriteString("12320") //Output //os.StdOut is *os.File //noReport = 12320 nr3h = 105 nr2h = 162 nr1h = 38 ok = 16899 //12320

对我来说很好。

请给我们你的代码。

UDP

string(noReport),noReport是整数吗? 这是预期的行为。

使用strconv.Itoa (noReport)。

noReport := 12320 nr3h := 105 nr2h := 162 nr1h := 38 ok := 16899 os.Stdout.WriteString("noReport = "+ strconv.Itoa(noReport) + " nr3h = " + strconv.Itoa(nr3h)+ " nr2h = "+ strconv.Itoa(nr2h)+ " nr1h = "+ strconv.Itoa(nr1h) + " ok = "+ strconv.Itoa(ok)+ "\n")

或者evanmcdonnal的回答 。

https://play.golang.org/p/QAXJ4aJBy3

fmt.Printf("os.StdOut is %T\n", os.Stdout) os.Stdout.WriteString("noReport = 12320 nr3h = 105 nr2h = 162 nr1h = 38 ok = 16899 \n") os.Stdout.WriteString("12320") //Output //os.StdOut is *os.File //noReport = 12320 nr3h = 105 nr2h = 162 nr1h = 38 ok = 16899 //12320

Works fine for me.

Give us your code, please.

UDP

string(noReport), noReport is integer right? Thats expected behavior.

Use strconv.Itoa(noReport).

noReport := 12320 nr3h := 105 nr2h := 162 nr1h := 38 ok := 16899 os.Stdout.WriteString("noReport = "+ strconv.Itoa(noReport) + " nr3h = " + strconv.Itoa(nr3h)+ " nr2h = "+ strconv.Itoa(nr2h)+ " nr1h = "+ strconv.Itoa(nr1h) + " ok = "+ strconv.Itoa(ok)+ "\n")

Or evanmcdonnal's answer.

更多推荐

本文发布于:2023-08-02 22:21:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1381937.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:转换为   字符串   文件   int   string

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!