package gopher_utils import ( "fmt" "strconv" ) // Convert string to specify type. type StrTo string func (f StrTo) Exist() bool { return string(f) != string(0x1E) } func (f StrTo) Uint8() (uint8, error) { v, err := strconv.ParseUint(f.String(), 10, 8) return uint8(v), err } func (f StrTo) Int() (int, error) { v, err := strconv.ParseInt(f.String(), 10, 0) return int(v), err } func (f StrTo) Int64() (int64, error) { v, err := strconv.ParseInt(f.String(), 10, 64) return int64(v), err } func (f StrTo) MustUint8() uint8 { v, _ := f.Uint8() return v } func (f StrTo) MustInt() int { v, _ := f.Int() return v } func (f StrTo) MustInt64() int64 { v, _ := f.Int64() return v } func (f StrTo) String() string { if f.Exist() { return string(f) } return "" } // RuneToStr converts rune to string. // Do not use `ToStr` function for a rune, because Go will understand it as an int32, so the string will contain, erroneously, the char code. func RuneToStr(value rune) string { return string(value) } // Convert any type to string. func ToStr(value interface{}, args ...int) (s string) { switch v := value.(type) { case bool: s = strconv.FormatBool(v) case float32: s = strconv.FormatFloat(float64(v), 'f', argInt(args).Get(0, -1), argInt(args).Get(1, 32)) case float64: s = strconv.FormatFloat(v, 'f', argInt(args).Get(0, -1), argInt(args).Get(1, 64)) case int: s = strconv.FormatInt(int64(v), argInt(args).Get(0, 10)) case int8: s = strconv.FormatInt(int64(v), argInt(args).Get(0, 10)) case int16: s = strconv.FormatInt(int64(v), argInt(args).Get(0, 10)) case int32: s = strconv.FormatInt(int64(v), argInt(args).Get(0, 10)) case int64: s = strconv.FormatInt(v, argInt(args).Get(0, 10)) case uint: s = strconv.FormatUint(uint64(v), argInt(args).Get(0, 10)) case uint8: s = strconv.FormatUint(uint64(v), argInt(args).Get(0, 10)) case uint16: s = strconv.FormatUint(uint64(v), argInt(args).Get(0, 10)) case uint32: s = strconv.FormatUint(uint64(v), argInt(args).Get(0, 10)) case uint64: s = strconv.FormatUint(v, argInt(args).Get(0, 10)) case string: s = v case []byte: s = string(v) default: s = fmt.Sprintf("%v", v) } return s } type argInt []int func (a argInt) Get(i int, args ...int) (r int) { if i >= 0 && i < len(a) { r = a[i] } else if len(args) > 0 { r = args[0] } return } // HexStr2int converts hex format string to decimal number. func HexStr2int(hexStr string) (int, error) { num := 0 length := len(hexStr) for i := 0; i < length; i++ { char := hexStr[length-i-1] factor := -1 switch { case char >= '0' && char <= '9': factor = int(char) - '0' case char >= 'a' && char <= 'f': factor = int(char) - 'a' + 10 default: return -1, fmt.Errorf("invalid hex: %s", string(char)) } num += factor * PowInt(16, i) } return num, nil } // Int2HexStr converts decimal number to hex format string. func Int2HexStr(num int) (hex string) { if num == 0 { return "0" } for num > 0 { r := num % 16 c := "?" if r >= 0 && r <= 9 { c = string(r + '0') } else { c = string(r + 'a' - 10) } hex = c + hex num = num / 16 } return hex }