61

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 needs an int. FormatInt needs an int64.

3
  • 4
    What is so bad about first converting the int32 to an int? Why do you need to avoid it? Commented Sep 12, 2016 at 6:42
  • 1
    Why is this a problem? Any formatting function you pass the value into will have to convert it internally anyway. Commented Sep 12, 2016 at 12:55
  • fmt.Sprint(i) or strconv.Itoa(int(i)) to set an int32 to a string. See stackoverflow.com/a/62737936/12817546. strconv.Atoi(s) and int32(i) to set a string to an int32. See stackoverflow.com/a/62740786/12817546. Commented Jul 9, 2020 at 5:00

5 Answers 5

119

One line answer is fmt.Sprint(i).

Anyway there are many conversions, even inside standard library function like fmt.Sprint(i), so you have some options (try The Go Playground):


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- 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- 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- 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
}

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

Code:

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:])
        }
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

The question was how to do it without converting n to another integer type.
@RolandIllig : See this new Edit.
14

The Sprint function converts a given value to string.

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)

See this example.

Comments

7

Use a conversion and strconv.FormatInt to format int32 values as a string. The conversion has zero cost on most platforms.

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

If you have many calls like this, consider writing a helper function similar to strconv.Itoa:

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

All of the low-level integer formatting code in the standard library works with int64 values. Any answer to this question using formatting code in the standard library (fmt package included) requires a conversion to int64 somewhere. The only way to avoid the conversion is to write formatting function from scratch, but there's little point in doing that.

Comments

1
func FormatInt32(value int32) string {
   return fmt.Sprintf("%d", value)
}

Does this work?

1 Comment

Please read How to Answer and edit your answer to contain an explanation as to why this code would actually solve the problem at hand. Always remember that you're not only solving the problem, but are also educating the OP and any future readers of this post.
0

I've implemented the following:

func Int32ToString(x int32) string {
  const base int32 = 10
  var remainder int32
  rtn_str := ""
  for x > 0 {
    remainder = x % base
    rtn_str = string(remainder + 48) + rtn_str
    x -= remainder
    x /= 10
  }
  return rtn_str
}

For 50000000 Iterations:

var x int32 = 234
var rtn_str string
for i := 0; i < 50000000; i++ {
    rtn_str = Int32ToString(x)
}

It took 3.289 seconds on my machine.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.