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?

FuzzyAmi
  • 7,543
  • 6
  • 45
  • 79
Sergei G
  • 1,561
  • 3
  • 18
  • 26
  • 3
    yes, convert to uint64 or int64, then encode to string. – thwd Oct 14 '15 at 18:58
  • Use fmt.Sprintf() to convert any integer type to string – Jishnu Prathap Dec 10 '16 at 09: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 https://stackoverflow.com/a/62740786/12817546. `string([]byte(s))` or `string([]rune(s))` to set a slice of bytes or runes to a string. See https://stackoverflow.com/a/62725637/12817546 and https://stackoverflow.com/a/62739051/12817546. –  Jul 08 '20 at 11:13

3 Answers3

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)
}
Jishnu Prathap
  • 1,955
  • 2
  • 21
  • 37
evanmcdonnal
  • 46,131
  • 16
  • 104
  • 115
  • 7
    `ParseInt` converts *from* a string to an integer. The question is asking about the other direction - converting *to* a string. – zmb Oct 14 '15 at 16:58
  • @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... – evanmcdonnal Oct 14 '15 at 17:01
  • 4
    How doesn't it? The question is titled "number to string conversion" and then says "strconv converts int64 and uint64 to string". – zmb Oct 14 '15 at 22:40
  • @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 ¯\_(ツ)_/¯ – evanmcdonnal Oct 14 '15 at 22:43
  • 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 :) – zmb Oct 14 '15 at 22:47
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"
peterSO
  • 158,998
  • 31
  • 281
  • 276
3

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

var num int64
numstring:=fmt.Sprintf("%d",num)
Scott Stensland
  • 26,870
  • 12
  • 93
  • 104
Jishnu Prathap
  • 1,955
  • 2
  • 21
  • 37
  • 1
    Top-notch answer that I quoted. Did I add to it? See https://stackoverflow.com/a/62725637/12817546 –  Jul 07 '20 at 21:41