2016-09-12 43 views
3

我需要將Golang中的int32轉換爲string。是否有可能在Golang中將int32轉換爲string而不先轉換爲intint64將int32轉換爲Golang中的字符串

Itoa需要intFormatInt需要int64

+4

什麼是壞對第一轉換' int32'到一個'int'?爲什麼你需要避免它? –

+1

這是一個什麼問題?無論如何,任何將值傳入的格式化函數都必須進行內部轉換。 – JimB

回答

34

單行解答是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:]) 
     } 
    } 
} 

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 
} 

3-您可以使用strconv.Itoa(int(i))快速
參見內:

// Itoa is shorthand for FormatInt(int64(i), 10). 
func Itoa(i int) string { 
    return FormatInt(int64(i), 10) 
} 

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 
} 

比較&基準(與5000次迭代):

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:]) 
     } 
    } 
} 
+0

問題是如何做到這一點_without_將'n'轉換爲另一種整數類型。 –

+0

@RolandIllig:看到這個新的編輯。 –

5

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) 

看到這個example

0

使用conversionstrconv.FormatInt將int32值格式化爲字符串。該轉換在大多數平臺上的成本爲零。

s := strconv.FormatInt(int64(n), 10) 

如果你有許多人呼籲這樣的,考慮寫類似於輔助函數來​​:

func formatInt32(n int32) string { 
    return strconv.FormatInt(int64(n), 10) 
} 

所有低級別的整數標準庫的作品格式化代碼int64值。使用標準庫中的格式化代碼(包括fmt軟件包)對此問題的任何回答都需要在某個地方使用conversionint64。避免轉換的唯一方法是從頭開始編寫格式化函數,但這樣做沒什麼意義。

相關問題