在Golang中将int32转换为字符串 [英] Convert int32 to string in Golang
问题描述
我需要在Golang中将int32
转换为string
.是否可以在Golang中将int32
转换为string
而不先转换为int
或int64
?
I need to convert an int32
to string
in Golang. Is it possible to convert int32
to string
in Golang without converting to int
or int64
first?
Itoa
需要一个int
. FormatInt
需要一个int64
.
Itoa
needs an int
. FormatInt
needs an int64
.
推荐答案
一个答案是fmt.Sprint(i)
.
无论如何,甚至在诸如fmt.Sprint(i)
之类的标准库函数内部,都有很多转换,因此您有一些选择(尝试 The Go游乐场):
Anyway there are many conversions, even inside standard library function like fmt.Sprint(i)
, so you have some options (try The Go Playground):
1-您可以编写转换函数(最快):
1- You may write your conversion function (Fastest):
func String(n int32) string {
buf := [11]byte{}
pos := len(buf)
i := int64(n)
signed := i < 0
if signed {
i = -i
}
for {
pos--
buf[pos], i = '0'+byte(i%10), i/10
if i == 0 {
if signed {
pos--
buf[pos] = '-'
}
return string(buf[pos:])
}
}
}
2-您可以使用fmt.Sprint(i)
(慢)
看到里面:
2- You may use fmt.Sprint(i)
(Slow)
See inside:
// Sprint formats using the default formats for its operands and returns the resulting string.
// Spaces are added between operands when neither is a string.
func Sprint(a ...interface{}) string {
p := newPrinter()
p.doPrint(a)
s := string(p.buf)
p.free()
return s
}
3-您可以使用strconv.Itoa(int(i))
(快速)
看到里面:
3- You may use strconv.Itoa(int(i))
(Fast)
See inside:
// Itoa is shorthand for FormatInt(int64(i), 10).
func Itoa(i int) string {
return FormatInt(int64(i), 10)
}
4-您可以使用strconv.FormatInt(int64(i), 10)
(更快)
看到里面:
4- You may use strconv.FormatInt(int64(i), 10)
(Faster)
See inside:
// FormatInt returns the string representation of i in the given base,
// for 2 <= base <= 36. The result uses the lower-case letters 'a' to 'z'
// for digit values >= 10.
func FormatInt(i int64, base int) string {
_, s := formatBits(nil, uint64(i), base, i < 0, false)
return s
}
比较&基准测试(具有50000000次迭代):
Comparison & Benchmark (with 50000000 iterations):
s = String(i) takes: 5.5923198s
s = String2(i) takes: 5.5923199s
s = strconv.FormatInt(int64(i), 10) takes: 5.9133382s
s = strconv.Itoa(int(i)) takes: 5.9763418s
s = fmt.Sprint(i) takes: 13.5697761s
代码:
package main
import (
"fmt"
//"strconv"
"time"
)
func main() {
var s string
i := int32(-2147483648)
t := time.Now()
for j := 0; j < 50000000; j++ {
s = String(i) //5.5923198s
//s = String2(i) //5.5923199s
//s = strconv.FormatInt(int64(i), 10) // 5.9133382s
//s = strconv.Itoa(int(i)) //5.9763418s
//s = fmt.Sprint(i) // 13.5697761s
}
fmt.Println(time.Since(t))
fmt.Println(s)
}
func String(n int32) string {
buf := [11]byte{}
pos := len(buf)
i := int64(n)
signed := i < 0
if signed {
i = -i
}
for {
pos--
buf[pos], i = '0'+byte(i%10), i/10
if i == 0 {
if signed {
pos--
buf[pos] = '-'
}
return string(buf[pos:])
}
}
}
func String2(n int32) string {
buf := [11]byte{}
pos := len(buf)
i, q := int64(n), int64(0)
signed := i < 0
if signed {
i = -i
}
for {
pos--
q = i / 10
buf[pos], i = '0'+byte(i-10*q), q
if i == 0 {
if signed {
pos--
buf[pos] = '-'
}
return string(buf[pos:])
}
}
}
这篇关于在Golang中将int32转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!