go-strconv

ParseInt

函数定义

func ParseInt(s string, base int, bitSize int) (i int64, err error) { }
base : 进制 比如2进制是2、10进制是10 、 以此类推
bitSize 预期数值的bit大小,用于数值上限限制,最终返回的还是int64类型

2进制例子

    strnum1 :="0101"
	toint,err1 := strconv.ParseInt(strnum1,2,32)
	fmt.Printf("toint type=%T, 且数字是%d \n",toint,toint);
	fmt.Printf("%s \n",err1)

输出

toint type=int64, 且数字是5

8进制例子

    strnum2 :="22"
	toint2,err2 := strconv.ParseInt(strnum2,8,32)
	fmt.Printf("toint type=%T, 且数字是%d \n",toint2,toint2);
	fmt.Printf("%s \n",err2)

输出

toint2 type=int64, 且数字是18

Atoi

字符串转化为10进制,和ParseInt(s, 10, 0)相等

函数定义

// Atoi is equivalent to ParseInt(s, 10, 0), converted to type int.
func Atoi(s string) (int, error) { }

    strnum := "123"
    // 把字符串变为十进制
	num,err := strconv.Atoi(strnum);
	fmt.Printf("num type=%T, 且数字是%d \n",num,num);

输出

num type=int, 且数字是123

Itoa

把十进制变为字符串

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

例子

    // Itoa把十进制变为字符串 string
	newNum := strconv.Itoa(num)
	fmt.Printf("newNum type=%T, value=%s \n",newNum,newNum)

FormatInt

数字变为字符串

函数定义

// 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.
// base是进制
func FormatInt(i int64, base int) string { }

数字变为2进制字符串

    var num3 int64;
	num3 = 7;
	str2num :=strconv.FormatInt(num3,2);
	fmt.Printf("str2num=%s \n",str2num)

输出

str2num=111

strconv.IntSize

输出IntSize当前操作系统的位数

strnum := "123"
	fmt.Printf("strnum type=%T, 当前占%d位 \n",strnum,strconv.IntSize);

输出

strnum type=string, 当前占64位

猜你喜欢

转载自blog.csdn.net/kq1983/article/details/112912916