golang strconv 包

strconv 包括 四 类函数

1.Append 类,例如 AppendBool(dst []byte, b bool)[]byte,将值转化后添加到[]byte的末尾

2.Format 类,例如FormatBool(b bool) string,将bool  float int uint 类型的转换为string,FormatInt的缩写为Itoa

3.Parse 类,例如ParseBool(str string)(value bool, err error) 将字符串转换为 bool float int uint类型的值,err指定是否转换成功,ParseInt的缩写是Atoi

4.Quote 类,对字符串的 双引号 单引号 反单引号 的操作

//main.go

package main

import (
    "fmt"
    "strconv"
)

func main() {

    dst := make([]byte, 2)
    fmt.Println(dst) //[0 0]
    //----------AppendBool
    //AppendBool将 布尔值转换为字符串,然后添加到dst的尾部
    //内部是实现:return append(dst, "true"...)
    rst := strconv.AppendBool(dst, true)
    fmt.Println(rst, string(rst)) //[0 0 116 114 117 101]   true
    rst = strconv.AppendBool(dst, false)
    fmt.Println(rst, string(rst)) //[0 0 102 97 108 115 101]   false
    //----------AppendBool
    f := 12345.123456789987654321
    dst = make([]byte, 2)
    fmt.Println(dst) //[0 0]
    //bitsize 参数只能是32或者64
    //使用math.Float64bits/math.Float32bits 函数转换到[]byte
    rst = strconv.AppendFloat(dst, f, 'f', 5, 32) //[0 0 49 50 51 52 53 46 49 50 51 48 53]12345.12305
    fmt.Println(rst, string(rst))
    rst = strconv.AppendFloat(dst, f, 'f', 5, 64) //[0 0 49 50 51 52 53 46 49 50 51 52 54]12345.12346
    fmt.Println(rst, string(rst))
    //----------AppendInt
    i := int64(128)
    dst = make([]byte, 2)
    fmt.Println(dst)                   //[0 0]
    rst = strconv.AppendInt(dst, i, 2) //[0 0 49 48 48 48 48 48 48 48]10000000
    fmt.Println(rst, string(rst))
    rst = strconv.AppendInt(dst, i, 8) //[0 0 50 48 48]200
    fmt.Println(rst, string(rst))
    rst = strconv.AppendInt(dst, i, 10) //[0 0 49 50 56]128
    fmt.Println(rst, string(rst))
    rst = strconv.AppendInt(dst, i, 16) //[0 0 49 50 56]128
    fmt.Println(rst, string(rst))       //[0 0 56 48]80
    //----------AppendQuote--AppendQuoteToASCII
    s := "你好世界"
    dst = make([]byte, 2)
    fmt.Println(dst)                  //[0 0]
    rst = strconv.AppendQuote(dst, s) //[0 0 34 104 101 108 108 111 32 119 111 114 108 100 34]"你好世界"
    fmt.Println(rst, string(rst))
    rst = strconv.AppendQuoteToASCII(dst, s) //[0 0 34 104 101 108 108 111 32 119 111 114 108 100 34]"\u4f60\u597d\u4e16\u754c"
    fmt.Println(rst, string(rst))
    //-----------AppendQuoteRune--AppendQuoteRuneToASCII
    r := '好'
    rst = strconv.AppendQuoteRune(dst, r) //[0 0 39 104 39]'好'
    fmt.Println(rst, string(rst))
    rst = strconv.AppendQuoteRuneToASCII(dst, r) //[0 0 39 104 39]'\u597d'
    fmt.Println(rst, string(rst))
    //----------AppendUint
    rst = strconv.AppendUint(dst, 4096, 16) //[0 0 49 48 48 48]1000
    fmt.Println(rst, string(rst))
    //----------Atoi
    //我的机器是64为的9,223,372,036,854,775,807 为int64的最大值点
    //当转换的值大于这个值的时候,会有一个err 并且val 被复制成最大值
    val, err := strconv.Atoi("9223372036854775809")
    //9223372036854775807 --- strconv.ParseInt: parsing "9223372036854775809": value out of range
    fmt.Println(val, "---", err)
    //----------CanBackquote
    s1 := "c:\\windows\n"
    ss := strconv.CanBackquote(s1)
    fmt.Println(ss) //false 其害还没明白他到底是做什么用的,知道朋友回一下,谢谢:)
    //----------FormatBool
    fmt.Println(strconv.FormatBool(1 < 2)) //ture
    //----------FormatFloat
    fmt.Println(strconv.FormatFloat(123.1233456, 'f', 6, 32)) //123.123344
    //----------FormarInt--FormatUint
    fmt.Println(strconv.FormatInt(15, 2))  //1111
    fmt.Println(strconv.FormatInt(15, 8))  //17
    fmt.Println(strconv.FormatInt(15, 10)) //15
    //----------IsPrint--是否是可打印字符
    fmt.Println(strconv.IsPrint('a'))  //true
    fmt.Println(strconv.IsPrint('\t')) //false
    //----------Itoa--shorthand for  FormatInt(i,10)
    fmt.Println(strconv.Itoa(123)) //123
    //----------ParseBool
    //"1", "t", "T", "true", "TRUE", "True":
    //"0", "f", "F", "false", "FALSE", "False":
    bb, err := strconv.ParseBool("f")
    fmt.Println(bb, err)
    //----------ParseFloat
    //func ParseFloat(s string, bitSize int) (f float64, err error)
    ff, err := strconv.ParseFloat("123.1234546789987654", 64)
    fmt.Println(ff, err)
    //----------ParseInt--ParseUint
    ii, err := strconv.ParseInt("A", 16, 32)
    fmt.Println(ii, err)
    //----------Quote--QuoteToASCII
    fmt.Println(strconv.Quote("hello world"))
    fmt.Println(strconv.QuoteToASCII("你好世界world"))
    //----------QuoteRune--QuoteRuneToASCII
    fmt.Println(strconv.QuoteRune('好'))
    fmt.Println(strconv.QuoteRuneToASCII('好'))
    //----------Unquote---UnquoteChar
    us, err := strconv.Unquote("\"sssssss\"")
    fmt.Println("\"sssssss\"")
    fmt.Println(us, err)
    us, err = strconv.Unquote(`'sssssss'`)
    fmt.Println("'sssssss'")
    fmt.Println(us, err)

}

 

posted @ 2014-11-07 17:46  yumuxu  阅读(1402)  评论(0编辑  收藏  举报