int--string
//string到int value_int,err:=strconv.Atoi(string) //int到string str:=strconv.Itoa(value_int)
int64--string
//string到int64 value_int64, err := strconv.ParseInt(string, 10, 64) //int64到string,需注意下面轉換規定 //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 str:=strconv.FormatInt(value_int64,10)
//FormatInt第二個參數表示進制,10表示十進制。
float--string
//float轉string
v := 3.1415926535
s1 := strconv.FormatFloat(v, 'E', -1, 32)//float32s2 := strconv.FormatFloat(v, 'E', -1, 64)//float64
//第二個參數可選'f'/'e'/'E'等,含義如下:
// 'b' (-ddddp±ddd,二進制指數)
// 'e' (-d.dddde±dd,十進制指數)
// 'E' (-d.ddddE±dd,十進制指數)
// 'f' (-ddd.dddd,沒有指數)
// 'g' ('e':大指數,'f':其它情況)
// 'G' ('E':大指數,'f':其它情況)
//string轉float
s := "3.1415926535"
v1, err := strconv.ParseFloat(v, 32)
v2, err := strconv.ParseFloat(v, 64)
float--int
//這個就很簡單了 var a int64 a = 1 var b float64 b = 2.000 //a -- float64 c := float64(a) //b -- int64 d := int64(b)
//interface{}到float64-------接口后加上 .(float64) //interface{}到string-------接口后加上 .(string)
下面是關於golang strconv的使用
// atob.go
------------------------------------------------------------
// ParseBool 將字符串轉換為布爾值
// 它接受真值:1, t, T, TRUE, true, True
// 它接受假值:0, f, F, FALSE, false, False.
// 其它任何值都返回一個錯誤
func ParseBool(str string) (value bool, err error)
func main() {
fmt.Println(strconv.ParseBool("1")) // true
fmt.Println(strconv.ParseBool("t")) // true
fmt.Println(strconv.ParseBool("T")) // true
fmt.Println(strconv.ParseBool("true")) // true
fmt.Println(strconv.ParseBool("True")) // true
fmt.Println(strconv.ParseBool("TRUE")) // true
fmt.Println(strconv.ParseBool("TRue")) // false strconv.ParseBool: parsing "TRue": invalid syntax
fmt.Println(strconv.ParseBool("0")) // false
fmt.Println(strconv.ParseBool("f")) // false
fmt.Println(strconv.ParseBool("F")) // false
fmt.Println(strconv.ParseBool("false")) // false
fmt.Println(strconv.ParseBool("False")) // false
fmt.Println(strconv.ParseBool("FALSE")) // false
fmt.Println(strconv.ParseBool("FALse")) // false strconv.ParseBool: parsing "FAlse": invalid syntax
}
------------------------------------------------------------
// FormatBool 將布爾值轉換為字符串 "true" 或 "false"
func FormatBool(b bool) string
func main() {
fmt.Println(strconv.FormatBool(0 < 1)) // true
fmt.Println(strconv.FormatBool(0 > 1)) // false
}
------------------------------------------------------------
// AppendBool 將布爾值 b 轉換為字符串 "true" 或 "false"
// 然后將結果追加到 dst 的尾部,返回追加后的 []bytefunc
AppendBool(dst []byte, b bool) []byte
func main() {
rst := make([]byte, 0)
rst = strconv.AppendBool(rst, 0 < 1)
fmt.Printf("%s\n", rst) // true
rst = strconv.AppendBool(rst, 0 > 1)
fmt.Printf("%s\n", rst) // truefalse
}
============================================================
// atof.go
------------------------------------------------------------
// ParseFloat 將字符串轉換為浮點數
// s:要轉換的字符串
// bitSize:指定浮點類型(32:float32、64:float64)
// 如果 s 是合法的格式,而且接近一個浮點值,
// 則返回浮點數的四舍五入值(依據 IEEE754 的四舍五入標准)
// 如果 s 不是合法的格式,則返回“語法錯誤”
// 如果轉換結果超出 bitSize 范圍,則返回“超出范圍”
func ParseFloat(s string, bitSize int) (f float64, err error)
func main() {
s := "0.12345678901234567890"
f, err := strconv.ParseFloat(s, 32)
fmt.Println(f, err) // 0.12345679104328156
fmt.Println(float32(f), err) // 0.12345679
f, err = strconv.ParseFloat(s, 64)
fmt.Println(f, err) // 0.12345678901234568
}
============================================================
// atoi.go
------------------------------------------------------------
// ErrRange 表示值超出范圍var ErrRange = errors.New("value out of range")
// ErrSyntax 表示語法不正確var ErrSyntax = errors.New("invalid syntax")
// NumError 記錄轉換失敗
type NumError struct {
Func string // 失敗的函數名(ParseBool, ParseInt, ParseUint, ParseFloat)
Num string // 輸入的值
Err error // 失敗的原因(ErrRange, ErrSyntax)
}
// int 或 uint 類型的長度(32 或 64)
const IntSize = intSize
const intSize = 32 << uint(^uint(0)>>63)
// 實現 Error 接口,輸出錯誤信息
func (e *NumError) Error() string
------------------------------------------------------------
// ParseInt 將字符串轉換為 int 類型
// s:要轉換的字符串
// base:進位制(2 進制到 36 進制)
// bitSize:指定整數類型(0:int、8:int8、16:int16、32:int32、64:int64)
// 返回轉換后的結果和轉換時遇到的錯誤
// 如果 base 為 0,則根據字符串的前綴判斷進位制(0x:16,0:8,其它:10)
func ParseInt(s string, base int, bitSize int) (i int64, err error)
func main() {
fmt.Println(strconv.ParseInt("123", 10, 8)) // 123
fmt.Println(strconv.ParseInt("12345", 10, 8)) // 127 strconv.ParseInt: parsing "12345": value out of range
fmt.Println(strconv.ParseInt("2147483647", 10, 0)) // 2147483647
fmt.Println(strconv.ParseInt("0xFF", 16, 0)) // 0 strconv.ParseInt: parsing "0xFF": invalid syntax
fmt.Println(strconv.ParseInt("FF", 16, 0)) // 255
fmt.Println(strconv.ParseInt("0xFF", 0, 0)) // 255
}
------------------------------------------------------------
// ParseUint 功能同 ParseInt 一樣,只不過返回 uint 類型整數
func ParseUint(s string, base int, bitSize int) (n uint64, err error)
func main() {
fmt.Println(strconv.ParseUint("FF", 16, 8)) // 255
}
------------------------------------------------------------
// Atoi 相當於 ParseInt(s, 10, 0)
// 通常使用這個函數,而不使用
ParseIntfunc Atoi(s string) (i int, err error)
func main() {
fmt.Println(strconv.Atoi("2147483647")) // 2147483647
fmt.Println(strconv.Atoi("2147483648")) // 2147483647 strconv.ParseInt: parsing "2147483648": value out of range
}
============================================================
// ftoa.go
------------------------------------------------------------
// FormatFloat 將浮點數 f 轉換為字符串值
// f:要轉換的浮點數
// fmt:格式標記(b、e、E、f、g、G)
// prec:精度(數字部分的長度,不包括指數部分)
// bitSize:指定浮點類型(32:float32、64:float64)
//// 格式標記:
// 'b' (-ddddp±ddd,二進制指數)
// 'e' (-d.dddde±dd,十進制指數)
// 'E' (-d.ddddE±dd,十進制指數)
// 'f' (-ddd.dddd,沒有指數)
// 'g' ('e':大指數,'f':其它情況)
// 'G' ('E':大指數,'f':其它情況)
//// 如果格式標記為 'e','E'和'f',則 prec 表示小數點后的數字位數
// 如果格式標記為 'g','G',則 prec 表示總的數字位數(整數部分+小數部分)
func FormatFloat(f float64, fmt byte, prec, bitSize int) string
func main() {
f := 100.12345678901234567890123456789
fmt.Println(strconv.FormatFloat(f, 'b', 5, 32)) // 13123382p-17
fmt.Println(strconv.FormatFloat(f, 'e', 5, 32)) // 1.00123e+02
fmt.Println(strconv.FormatFloat(f, 'E', 5, 32)) // 1.00123E+02
fmt.Println(strconv.FormatFloat(f, 'f', 5, 32)) // 100.12346
fmt.Println(strconv.FormatFloat(f, 'g', 5, 32)) // 100.12
fmt.Println(strconv.FormatFloat(f, 'G', 5, 32)) // 100.12
fmt.Println(strconv.FormatFloat(f, 'b', 30, 32)) // 13123382p-17
fmt.Println(strconv.FormatFloat(f, 'e', 30, 32)) // 1.001234588623046875000000000000e+02
fmt.Println(strconv.FormatFloat(f, 'E', 30, 32)) // 1.001234588623046875000000000000E+02
fmt.Println(strconv.FormatFloat(f, 'f', 30, 32)) // 100.123458862304687500000000000000
fmt.Println(strconv.FormatFloat(f, 'g', 30, 32)) // 100.1234588623046875
fmt.Println(strconv.FormatFloat(f, 'G', 30, 32)) // 100.1234588623046875
}
------------------------------------------------------------
// AppendFloat 將浮點數 f 轉換為字符串值,並將轉換結果追加到 dst 的尾部
// 返回追加后的 []bytefunc
AppendFloat(dst []byte, f float64, fmt byte, prec int, bitSize int) []byte
func main() {
f := 100.12345678901234567890123456789
b := make([]byte, 0)
b = strconv.AppendFloat(b, f, 'f', 5, 32)
b = append(b, " "...)
b = strconv.AppendFloat(b, f, 'e', 5, 32)
fmt.Printf("%s", b) // 100.12346 1.00123e+0
}
============================================================
// itoa.go
------------------------------------------------------------
// FormatUint 將 int 型整數 i 轉換為字符串形式
// base:進位制(2 進制到 36 進制)
// 大於 10 進制的數,返回值使用小寫字母 'a' 到 'z'func
FormatInt(i int64, base int) string
func main() {
i := int64(-2048)
fmt.Println(strconv.FormatInt(i, 2)) // -100000000000
fmt.Println(strconv.FormatInt(i, 8)) // -4000
fmt.Println(strconv.FormatInt(i, 10)) // -2048
fmt.Println(strconv.FormatInt(i, 16)) // -800
fmt.Println(strconv.FormatInt(i, 36)) // -1kw
}
------------------------------------------------------------
// FormatUint 將 uint 型整數 i 轉換為字符串形式
// base:進位制(2 進制到 36 進制)
// 大於 10 進制的數,返回值使用小寫字母 'a' 到 'z'func
FormatUint(i uint64, base int) string
func main() {
i := uint64(2048)
fmt.Println(strconv.FormatUint(i, 2)) // 100000000000
fmt.Println(strconv.FormatUint(i, 8)) // 4000
fmt.Println(strconv.FormatUint(i, 10)) // 2048
fmt.Println(strconv.FormatUint(i, 16)) // 800
fmt.Println(strconv.FormatUint(i, 36)) // 1kw
}
------------------------------------------------------------
// Itoa 相當於 FormatInt(i, 10)func Itoa(i int) string
func main() {
fmt.Println(strconv.Itoa(-2048)) // -2048
fmt.Println(strconv.Itoa(2048)) // 2048
}
------------------------------------------------------------
// AppendInt 將 int 型整數 i 轉換為字符串形式,並追加到 dst 的尾部
// i:要轉換的字符串
// base:進位制
// 返回追加后的 []bytefunc
AppendInt(dst []byte, i int64, base int) []byte
func main() {
b := make([]byte, 0)
b = strconv.AppendInt(b, -2048, 16)
fmt.Printf("%s", b) // -800
}
------------------------------------------------------------
// AppendUint 將 uint 型整數 i 轉換為字符串形式,並追加到 dst 的尾部
// i:要轉換的字符串
// base:進位制
// 返回追加后的 []bytefunc
AppendUint(dst []byte, i uint64, base int) []byte
func main() {
b := make([]byte, 0)
b = strconv.AppendUint(b, 2048, 16)
fmt.Printf("%s", b) // 800
}
============================================================
// quote.go
------------------------------------------------------------
// Quote 將字符串 s 轉換為“雙引號”引起來的字符串
// 其中的特殊字符將被轉換為“轉義字符”
// “不可顯示的字符”將被轉換為“轉義字符”
func Quote(s string) string
func main() {
fmt.Println(strconv.Quote(`C:\Windows`)) // "C:\\Windows"
}
------------------------------------------------------------
// AppendQuote 將字符串 s 轉換為“雙引號”引起來的字符串,
// 並將結果追加到 dst 的尾部,返回追加后的 []byte
// 其中的特殊字符將被轉換為“轉義字符”func
AppendQuote(dst []byte, s string) []byte
func main() {
s := `C:\Windows`
b := make([]byte, 0)
b = strconv.AppendQuote(b, s)
fmt.Printf("%s", b) // "C:\\Windows"
}
------------------------------------------------------------
// QuoteToASCII 將字符串 s 轉換為“雙引號”引起來的 ASCII 字符串
// “非 ASCII 字符”和“特殊字符”將被轉換為“轉義字符”
func QuoteToASCII(s string) string
func main() {
fmt.Println(strconv.QuoteToASCII("Hello 世界!")) // "Hello \u4e16\u754c\uff01"
}
------------------------------------------------------------
// AppendQuoteToASCII 將字符串 s 轉換為“雙引號”引起來的 ASCII 字符串,
// 並將結果追加到 dst 的尾部,返回追加后的 []byte
// “非 ASCII 字符”和“特殊字符”將被轉換為“轉義字符”
func AppendQuoteToASCII(dst []byte, s string) []byte
func main() {
s := "Hello 世界!"
b := make([]byte, 0)
b = strconv.AppendQuoteToASCII(b, s)
fmt.Printf("%s", b) // "Hello \u4e16\u754c\uff01"
}
------------------------------------------------------------
// QuoteRune 將 Unicode 字符轉換為“單引號”引起來的字符串
// “特殊字符”將被轉換為“轉義字符”
func QuoteRune(r rune) string
func main() {
fmt.Println(strconv.QuoteRune('好')) // '好'
}
------------------------------------------------------------
// AppendQuoteRune 將 Unicode 字符轉換為“單引號”引起來的字符串,
// 並將結果追加到 dst 的尾部,返回追加后的 []byte
// “特殊字符”將被轉換為“轉義字符”
func AppendQuoteRune(dst []byte, r rune) []byte
func main() {
b := make([]byte, 0)
b = strconv.AppendQuoteRune(b, '好')
fmt.Printf("%s", b) // '好'
}
------------------------------------------------------------
// QuoteRuneToASCII 將 Unicode 字符轉換為“單引號”引起來的 ASCII 字符串
// “非 ASCII 字符”和“特殊字符”將被轉換為“轉義字符”
func QuoteRuneToASCII(r rune) string
func main() {
fmt.Println(strconv.QuoteRuneToASCII('好')) // '\u597d'
}
------------------------------------------------------------
// AppendQuoteRune 將 Unicode 字符轉換為“單引號”引起來的 ASCII 字符串,
// 並將結果追加到 dst 的尾部,返回追加后的 []byte
// “非 ASCII 字符”和“特殊字符”將被轉換為“轉義字符”
func AppendQuoteRuneToASCII(dst []byte, r rune) []byte
func main() {
b := make([]byte, 0)
b = strconv.AppendQuoteRuneToASCII(b, '好')
fmt.Printf("%s", b) // '\u597d'
}
------------------------------------------------------------
// CanBackquote 判斷字符串 s 是否可以表示為一個單行的“反引號”字符串
// 字符串中不能含有控制字符(除了 \t)和“反引號”字符,否則返回 false
func CanBackquote(s string) bool
func main() {
b := strconv.CanBackquote("C:\\Windows\n")
fmt.Println(b) // false
b = strconv.CanBackquote("C:\\Windows\r")
fmt.Println(b) // false
b = strconv.CanBackquote("C:\\Windows\f")
fmt.Println(b) // false
b = strconv.CanBackquote("C:\\Windows\t")
fmt.Println(b) // true
b = strconv.CanBackquote("C:\\`Windows`")
fmt.Println(b) // false
}
------------------------------------------------------------
// UnquoteChar 將 s 中的第一個字符“取消轉義”並解碼
//// s:轉義后的字符串
// quote:字符串使用的“引號符”(用於對引號符“取消轉義”)
//// value: 解碼后的字符
// multibyte:value 是否為多字節字符
// tail: 字符串 s 除去 value 后的剩余部分
// error: 返回 s 中是否存在語法錯誤
//// 參數 quote 為“引號符”
// 如果設置為單引號,則 s 中允許出現 \' 字符,不允許出現單獨的 ' 字符
// 如果設置為雙引號,則 s 中允許出現 \" 字符,不允許出現單獨的 " 字符
// 如果設置為 0,則不允許出現 \' 或 \" 字符,可以出現單獨的 ' 或 " 字符
func UnquoteChar(s string, quote byte) (value rune, multibyte bool, tail string, err error)
func main() {
s := `\"大\\家\\好!\"`
c, mb, sr, _ := strconv.UnquoteChar(s, '"')
fmt.Printf("%-3c %v\n", c, mb) for ; len(sr) > 0; c, mb, sr, _ = strconv.UnquoteChar(sr, '"') {
fmt.Printf("%-3c %v\n", c, mb)
} // " false
// 大 true
// \ false
// 家 true
// \ false
// 好 true
// ! true
}
------------------------------------------------------------
// Unquote 將“帶引號的字符串” s 轉換為常規的字符串(不帶引號和轉義字符)
// s 可以是“單引號”、“雙引號”或“反引號”引起來的字符串(包括引號本身)
// 如果 s 是單引號引起來的字符串,則返回該該字符串代表的字符
func Unquote(s string) (t string, err error)
func main() {
sr, err := strconv.Unquote(`"\"大\t家\t好!\""`)
fmt.Println(sr, err)
sr, err = strconv.Unquote(`'大家好!'`)
fmt.Println(sr, err)
sr, err = strconv.Unquote(`'好'`)
fmt.Println(sr, err)
sr, err = strconv.Unquote("`大\\t家\\t好!`")
fmt.Println(sr, err)
}
------------------------------------------------------------
// IsPrint 判斷 Unicode 字符 r 是否是一個可顯示的字符
// 可否顯示並不是你想象的那樣,比如空格可以顯示,而\t則不能顯示
// 具體可以參考 Go 語言的源碼
func IsPrint(r rune) bool
func main() {
fmt.Println(strconv.IsPrint('a')) // true
fmt.Println(strconv.IsPrint('好')) // true
fmt.Println(strconv.IsPrint(' ')) // true
fmt.Println(strconv.IsPrint('\t')) // false
fmt.Println(strconv.IsPrint('\n')) // false
fmt.Println(strconv.IsPrint(0)) // false
}
