convert.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. // Copyright 2014 com authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License"): you may
  4. // not use this file except in compliance with the License. You may obtain
  5. // a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  11. // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  12. // License for the specific language governing permissions and limitations
  13. // under the License.
  14. package com
  15. import (
  16. "fmt"
  17. "strconv"
  18. )
  19. // Convert string to specify type.
  20. type StrTo string
  21. func (f StrTo) Exist() bool {
  22. return string(f) != string(0x1E)
  23. }
  24. func (f StrTo) Uint8() (uint8, error) {
  25. v, err := strconv.ParseUint(f.String(), 10, 8)
  26. return uint8(v), err
  27. }
  28. func (f StrTo) Int() (int, error) {
  29. v, err := strconv.ParseInt(f.String(), 10, 0)
  30. return int(v), err
  31. }
  32. func (f StrTo) Int64() (int64, error) {
  33. v, err := strconv.ParseInt(f.String(), 10, 64)
  34. return int64(v), err
  35. }
  36. func (f StrTo) Float64() (float64, error) {
  37. v, err := strconv.ParseFloat(f.String(), 64)
  38. return float64(v), err
  39. }
  40. func (f StrTo) MustUint8() uint8 {
  41. v, _ := f.Uint8()
  42. return v
  43. }
  44. func (f StrTo) MustInt() int {
  45. v, _ := f.Int()
  46. return v
  47. }
  48. func (f StrTo) MustInt64() int64 {
  49. v, _ := f.Int64()
  50. return v
  51. }
  52. func (f StrTo) MustFloat64() float64 {
  53. v, _ := f.Float64()
  54. return v
  55. }
  56. func (f StrTo) String() string {
  57. if f.Exist() {
  58. return string(f)
  59. }
  60. return ""
  61. }
  62. // Convert any type to string.
  63. func ToStr(value interface{}, args ...int) (s string) {
  64. switch v := value.(type) {
  65. case bool:
  66. s = strconv.FormatBool(v)
  67. case float32:
  68. s = strconv.FormatFloat(float64(v), 'f', argInt(args).Get(0, -1), argInt(args).Get(1, 32))
  69. case float64:
  70. s = strconv.FormatFloat(v, 'f', argInt(args).Get(0, -1), argInt(args).Get(1, 64))
  71. case int:
  72. s = strconv.FormatInt(int64(v), argInt(args).Get(0, 10))
  73. case int8:
  74. s = strconv.FormatInt(int64(v), argInt(args).Get(0, 10))
  75. case int16:
  76. s = strconv.FormatInt(int64(v), argInt(args).Get(0, 10))
  77. case int32:
  78. s = strconv.FormatInt(int64(v), argInt(args).Get(0, 10))
  79. case int64:
  80. s = strconv.FormatInt(v, argInt(args).Get(0, 10))
  81. case uint:
  82. s = strconv.FormatUint(uint64(v), argInt(args).Get(0, 10))
  83. case uint8:
  84. s = strconv.FormatUint(uint64(v), argInt(args).Get(0, 10))
  85. case uint16:
  86. s = strconv.FormatUint(uint64(v), argInt(args).Get(0, 10))
  87. case uint32:
  88. s = strconv.FormatUint(uint64(v), argInt(args).Get(0, 10))
  89. case uint64:
  90. s = strconv.FormatUint(v, argInt(args).Get(0, 10))
  91. case string:
  92. s = v
  93. case []byte:
  94. s = string(v)
  95. default:
  96. s = fmt.Sprintf("%v", v)
  97. }
  98. return s
  99. }
  100. type argInt []int
  101. func (a argInt) Get(i int, args ...int) (r int) {
  102. if i >= 0 && i < len(a) {
  103. r = a[i]
  104. } else if len(args) > 0 {
  105. r = args[0]
  106. }
  107. return
  108. }
  109. // HexStr2int converts hex format string to decimal number.
  110. func HexStr2int(hexStr string) (int, error) {
  111. num := 0
  112. length := len(hexStr)
  113. for i := 0; i < length; i++ {
  114. char := hexStr[length-i-1]
  115. factor := -1
  116. switch {
  117. case char >= '0' && char <= '9':
  118. factor = int(char) - '0'
  119. case char >= 'a' && char <= 'f':
  120. factor = int(char) - 'a' + 10
  121. default:
  122. return -1, fmt.Errorf("invalid hex: %s", string(char))
  123. }
  124. num += factor * PowInt(16, i)
  125. }
  126. return num, nil
  127. }
  128. // Int2HexStr converts decimal number to hex format string.
  129. func Int2HexStr(num int) (hex string) {
  130. if num == 0 {
  131. return "0"
  132. }
  133. for num > 0 {
  134. r := num % 16
  135. c := "?"
  136. if r >= 0 && r <= 9 {
  137. c = string(r + '0')
  138. } else {
  139. c = string(r + 'a' - 10)
  140. }
  141. hex = c + hex
  142. num = num / 16
  143. }
  144. return hex
  145. }