在Golang中将int32转换为字符串

cod*_*efx 17 go

我需要在Golang中转换int32string.是否有可能转换int32stringGolang而不转换为intint64首先?

Itoa需要一个int.FormatInt需要一个int64.

小智 57

一线答案是fmt.Sprint(i).

无论如何,有很多转换,甚至在标准库函数内部fmt.Sprint(i),所以你有一些选择(尝试The Go Playground):


1-您可以编写转换函数(最快):

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:])
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

2-你可以使用fmt.Sprint(i) ()
见里面:

// 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
}
Run Code Online (Sandbox Code Playgroud)

3-您可以使用strconv.Itoa(int(i)) (快速)
见内部:

// Itoa is shorthand for FormatInt(int64(i), 10).
func Itoa(i int) string {
    return FormatInt(int64(i), 10)
}
Run Code Online (Sandbox Code Playgroud)

4-你可以使用strconv.FormatInt(int64(i), 10)(更快)
见里面:

// 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
}
Run Code Online (Sandbox Code Playgroud)

比较和基准(具有50000000次迭代):

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
Run Code Online (Sandbox Code Playgroud)

码:

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:])
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


Ben*_*ell 8

Sprint的功能转换给定值的字符串.

package main

import (
     "fmt"
)

func main() {

      var sampleInt int32 = 1

      sampleString := fmt.Sprint(sampleInt)
      fmt.Printf("%+V %+V\n", sampleInt, sampleString)
}

// %!V(int32=+1) %!V(string=1)
Run Code Online (Sandbox Code Playgroud)

看这个例子.


Cer*_*món 7

使用转换strconv.FormatInt将 int32 值格式化为字符串。在大多数平台上,转换成本为零。

s := strconv.FormatInt(int64(n), 10)
Run Code Online (Sandbox Code Playgroud)

如果您有很多这样的调用,请考虑编写一个类似于strconv.Itoa 的辅助函数:

func formatInt32(n int32) string {
    return strconv.FormatInt(int64(n), 10)
}
Run Code Online (Sandbox Code Playgroud)

标准库中的所有低级整数格式化代码都适用于int64值。任何使用标准库(包括 fmt 包)中的格式化代码来回答这个问题都需要转换int64某个地方。避免转换的唯一方法是从头开始编写格式化函数,但这样做没有什么意义。