1. string转int

func TestString2Int(t *testing.T){
    var valS string
    valS = "180"
    valInt, err := strconv.Atoi(valS)   // 函数原型 :func Atoi(s string) (int, error)
    if err != nil{
        t.Fatalf("convert string to int failed, err=%s.", err)
    }
    t.Logf("int val=%d, val type: [%s]\n", valInt, reflect.TypeOf(valInt))
}

运行结果

=== RUN   TestString2Int
--- PASS: TestString2Int (0.00s)
    typeConversion_test.go:17: int val=180, val type: [int]

如果 valS大于int所能表示的范围,比如valS = “18446744073709551626”, 2的64次方加10,则会出错

=== RUN   TestString2Int
--- FAIL: TestString2Int (0.00s)
   typeConversion_test.go:15: convert string to int failed, err=strconv.Atoi: parsing "18446744073709551626": value out of range.

2. string转int64

func TestString2Int64(t *testing.T){
    var valS string
    valS = "180"
    valInt64,err := strconv.ParseInt(valS, 10, 64)
    if err != nil{
        t.Fatalf("convert string to int failed, err=%s.", err)
    }
    t.Logf("int val=%d, val type: [%s]\n", valInt64, reflect.TypeOf(valInt64))
}

特别说明

上述函数原型为:func ParseInt(s string, base int, bitSize int) (i int64, err error)
返回值类型为int64, 故此方法并不能真正的将string类型转为int32类型而返回

参数说明

  • s:要转换的字符串。
  • base:字符串内容的进制数,如八进制、十进制、十六进制,此处规定了s中都可以包含哪些字符,假如base=8,则字符串中只能出现0-7的数字;base=16,则可以出现0-f的字符。
  • bitSize: 其实规定了字符串所能表示的最大数值,比如bitSize=32, 则表示s代表的值不能大于2的32次方。</font>

其他举例

  • 字符串为十六进制数,且base参数设置正确
func TestString2Int64(t *testing.T){
    var valS string
    valS = "1e"
    valInt64,err := strconv.ParseInt(valS, 16, 64)
    if err != nil{
        t.Fatalf("convert string to int failed, err=%s.", err)
    }
    t.Logf("int val=%d, val type: [%s]\n", valInt64, reflect.TypeOf(valInt64))
}
//运行结果
=== RUN   TestString2Int64
--- PASS: TestString2Int64 (0.00s)
    typeConversion_test.go:28: int val=30, val type: [int64]
  • 字符串为十六进制数,且base参数设置错误
func TestString2Int64(t *testing.T){
    var valS string
    valS = "1e"
    valInt64,err := strconv.ParseInt(valS, 10, 64)
    if err != nil{
        t.Fatalf("convert string to int failed, err=%s.", err)
    }
    t.Logf("int val=%d, val type: [%s]\n", valInt64, reflect.TypeOf(valInt64))
}
//运行结果
=== RUN   TestString2Int64
--- FAIL: TestString2Int64 (0.00s)
    typeConversion_test.go:26: convert string to int failed, err=strconv.ParseInt: parsing "1e": invalid syntax.

  • 字符串为十进制数,超出2的32次方
func TestString2Int64(t *testing.T){
    var valS string
    valS = "4294967396"
    valInt64,err := strconv.ParseInt(valS, 10, 32)
    if err != nil{
        t.Fatalf("convert string to int failed, err=%s.", err)
    }
    t.Logf("int val=%d, val type: [%s]\n", valInt64, reflect.TypeOf(valInt64))
}
//运行结果
=== RUN   TestString2Int64
--- FAIL: TestString2Int64 (0.00s)
    typeConversion_test.go:26: convert string to int failed, err=strconv.ParseInt: parsing "4294967396": value out of range.

3. string转int32

可以先通过strconv.ParseInt转换成int64,再强转成int32,转int64时需要用bitSize=32,确保字符串表示的值在int32可表示的值范围内。

func TestString2Int64(t *testing.T){
    var valS string
    valS = "89"
    valInt64,err := strconv.ParseInt(valS, 10, 32)
    if err != nil{
        t.Fatalf("convert string to int failed, err=%s.", err)
    }
    t.Logf("int val=%d, val type: [%s]\n", valInt64, reflect.TypeOf(valInt64))
    valInt32 := int32(valInt64)
    t.Logf("int val=%d, val type: [%s]\n", valInt32, reflect.TypeOf(valInt32))
}
//运行结果
=== RUN   TestString2Int64
--- PASS: TestString2Int64 (0.00s)
    typeConversion_test.go:28: int val=89, val type: [int64]
    typeConversion_test.go:30: int val=89, val type: [int32]