写入文件 int 转换为字符串

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

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

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


胡说叔叔
浏览 132回答 2
2回答

慕莱坞森

要使用您的整数获取字符串,我建议使用 fmt.Sprintf会是这样的;s := fmt.Sprintf("noReport = %d nr3h = %d nr2h = %d nr1h = %d ok = 16899", 12320, 162, 38)这会将值分配"noReport = 12320 nr3h = 105 nr2h = 162 nr1h = 38 ok = 16899"给s.

慕无忌1623718

https://play.golang.org/p/QAXJ4aJBy3fmt.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对我来说很好。请给我们您的代码。UDPstring(noReport), noReport 是整数吗?那是预期的行为。使用strconv.Itoa(无报告)。noReport := 12320nr3h := 105nr2h := 162nr1h := 38ok := 16899os.Stdout.WriteString("noReport = "+ strconv.Itoa(noReport) + " nr3h = " + strconv.Itoa(nr3h)+ " nr2h = "+ strconv.Itoa(nr2h)+ " nr1h = "+ strconv.Itoa(nr1h) + " ok = "+ strconv.Itoa(ok)+ "\n") 或 evanmcdonnal 的回答。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go