4

Golang has strconv library that converts string to int64 and uint64.

However, the rest of integer data types seems to be unsupported as I can't find conversion functions for byte, int16, uint16, int32, uint32 data types.

One can always convert from byte, 16-bit and 32-bit data types to int64 and uint64 without loss of precision. Is that what's intended by language?

3
  • 3
    yes, convert to uint64 or int64, then encode to string. Commented Oct 14, 2015 at 18:58
  • Use fmt.Sprintf() to convert any integer type to string Commented Dec 10, 2016 at 9:44
  • []byte(s) to set a string to a slice of uint8 or []rune(s) to set a string to a slice of int32. See stackoverflow.com/a/62740786/12817546. string([]byte(s)) or string([]rune(s)) to set a slice of bytes or runes to a string. See stackoverflow.com/a/62725637/12817546 and stackoverflow.com/a/62739051/12817546. Commented Jul 8, 2020 at 11:13

3 Answers 3

7

If you look at the docs a bit more closely you can use this method;

func ParseInt(s string, base int, bitSize int)

https://golang.org/pkg/strconv/#ParseInt

The bitSize argument says how large the int is so you can do 8 or 16 or 32 for those smaller integer types. Atoi calls this internally. I believe you're wanting 10 for the base argument. So like b, err := strconv.ParseInt("5", 10, 8) for a byte.

EDIT: Just going to add a couple things to the answer here in case the OP is in fact confused how to convert a 16-bit int into a string... If that is your intended goal just use fmt.Sprintf or you can do a conversion from smaller int to larger int as it will always succeed. Examples of both here;

package main

import "fmt"
import "strconv"

func main() {
    var a int16
    a = 5
    s := fmt.Sprintf("%d", a)
    s2 := strconv.Itoa(int(a))
    fmt.Println(s)
    fmt.Println(s2)
}
Sign up to request clarification or add additional context in comments.

5 Comments

ParseInt converts from a string to an integer. The question is asking about the other direction - converting to a string.
@zmb I highly doubt that. The issue of size is only relevant when going from a string to the number types. Strings are unbounded in size (except by your cpu's memory) so I can obviously convert any type of number in the language to a string without having to consider any of this. I also, don't need to use a special library like strconv the methods in fmt are perfectly sufficient. Lastly, if I have an 32-bit int and a 'itoa' method that only works for 64-bit as the OP points out it's very obvious what needs to be done which is a cast to (int64) which will always succeed...
How doesn't it? The question is titled "number to string conversion" and then says "strconv converts int64 and uint64 to string".
@zmb I assumed the OP is confused due to that question not making any sense. The amount of bytes occupied by a number obviously has no bearing on the conversion to string... Anyway, in response to your comment I updated to explain conversions in either direction then got downvoted ¯_(ツ)_/¯
I think the OP is confused too, just for a different reason. Perhaps (s)he doesn't know about the fmt package. You've got my upvote in either case :)
3

For example,

package main

import (
    "fmt"
    "strconv"
)

func main() {
    n := int16(42)
    s := strconv.FormatInt(int64(n), 10)
    fmt.Printf("n %d s %q\n", n, s)
}

Output:

n 42 s "42"

Comments

3

You can convert any int type to string using fmt.Sprintf() method

var num int64
numstring:=fmt.Sprintf("%d",num)

1 Comment

Top-notch answer that I quoted. Did I add to it? See stackoverflow.com/a/62725637/12817546

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.