convert.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. package utils
  2. import (
  3. "fmt"
  4. "strconv"
  5. )
  6. // Convert string to specify type.
  7. type StrTo string
  8. func (f StrTo) Exist() bool {
  9. return string(f) != string(rune(0x1E))
  10. }
  11. func (f StrTo) Uint8() (uint8, error) {
  12. v, err := strconv.ParseUint(f.String(), 10, 8)
  13. return uint8(v), err
  14. }
  15. func (f StrTo) Int() (int, error) {
  16. v, err := strconv.ParseInt(f.String(), 10, 0)
  17. return int(v), err
  18. }
  19. func (f StrTo) Int64() (int64, error) {
  20. v, err := strconv.ParseInt(f.String(), 10, 64)
  21. return int64(v), err
  22. }
  23. func (f StrTo) Float64() (float64, error) {
  24. v, err := strconv.ParseFloat(f.String(), 64)
  25. return float64(v), err
  26. }
  27. func (f StrTo) MustUint8() uint8 {
  28. v, _ := f.Uint8()
  29. return v
  30. }
  31. func (f StrTo) MustInt() int {
  32. v, _ := f.Int()
  33. return v
  34. }
  35. func (f StrTo) MustInt64() int64 {
  36. v, _ := f.Int64()
  37. return v
  38. }
  39. func (f StrTo) MustFloat64() float64 {
  40. v, _ := f.Float64()
  41. return v
  42. }
  43. func (f StrTo) String() string {
  44. if f.Exist() {
  45. return string(f)
  46. }
  47. return ""
  48. }
  49. func ToInt(value interface{}, args ...int) (i int) {
  50. return StrTo(ToStr(value, args...)).MustInt()
  51. }
  52. func ToInt64(value interface{}, args ...int) (i int64) {
  53. return StrTo(ToStr(value, args...)).MustInt64()
  54. }
  55. func ToFloat64(value interface{}, args ...int) (i float64) {
  56. return StrTo(ToStr(value, args...)).MustFloat64()
  57. }
  58. // Convert any type to string.
  59. func ToStr(value interface{}, args ...int) (s string) {
  60. switch v := value.(type) {
  61. case bool:
  62. s = strconv.FormatBool(v)
  63. case float32:
  64. s = strconv.FormatFloat(float64(v), 'f', argInt(args).Get(0, -1), argInt(args).Get(1, 32))
  65. case float64:
  66. s = strconv.FormatFloat(v, 'f', argInt(args).Get(0, -1), argInt(args).Get(1, 64))
  67. case int:
  68. s = strconv.FormatInt(int64(v), argInt(args).Get(0, 10))
  69. case int8:
  70. s = strconv.FormatInt(int64(v), argInt(args).Get(0, 10))
  71. case int16:
  72. s = strconv.FormatInt(int64(v), argInt(args).Get(0, 10))
  73. case int32:
  74. s = strconv.FormatInt(int64(v), argInt(args).Get(0, 10))
  75. case int64:
  76. s = strconv.FormatInt(v, argInt(args).Get(0, 10))
  77. case uint:
  78. s = strconv.FormatUint(uint64(v), argInt(args).Get(0, 10))
  79. case uint8:
  80. s = strconv.FormatUint(uint64(v), argInt(args).Get(0, 10))
  81. case uint16:
  82. s = strconv.FormatUint(uint64(v), argInt(args).Get(0, 10))
  83. case uint32:
  84. s = strconv.FormatUint(uint64(v), argInt(args).Get(0, 10))
  85. case uint64:
  86. s = strconv.FormatUint(v, argInt(args).Get(0, 10))
  87. case string:
  88. s = v
  89. case []byte:
  90. s = string(v)
  91. case nil:
  92. s = ""
  93. default:
  94. s = fmt.Sprintf("%v", v)
  95. }
  96. return s
  97. }
  98. type argInt []int
  99. func (a argInt) Get(i int, args ...int) (r int) {
  100. if i >= 0 && i < len(a) {
  101. r = a[i]
  102. } else if len(args) > 0 {
  103. r = args[0]
  104. }
  105. return
  106. }
  107. // HexStr2int converts hex format string to decimal number.
  108. func HexStr2int(hexStr string) (int, error) {
  109. num := 0
  110. length := len(hexStr)
  111. for i := 0; i < length; i++ {
  112. char := hexStr[length-i-1]
  113. factor := -1
  114. switch {
  115. case char >= '0' && char <= '9':
  116. factor = int(char) - '0'
  117. case char >= 'a' && char <= 'f':
  118. factor = int(char) - 'a' + 10
  119. default:
  120. return -1, fmt.Errorf("invalid hex: %s", string(char))
  121. }
  122. num += factor * PowInt(16, i)
  123. }
  124. return num, nil
  125. }
  126. // Int2HexStr converts decimal number to hex format string.
  127. func Int2HexStr(num int) (hex string) {
  128. if num == 0 {
  129. return "0"
  130. }
  131. for num > 0 {
  132. r := num % 16
  133. c := "?"
  134. if r >= 0 && r <= 9 {
  135. c = string(rune(r + '0'))
  136. } else {
  137. c = string(rune(r + 'a' - 10))
  138. }
  139. hex = c + hex
  140. num = num / 16
  141. }
  142. return hex
  143. }