convert.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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) MustUint8() uint8 {
  37. v, _ := f.Uint8()
  38. return v
  39. }
  40. func (f StrTo) MustInt() int {
  41. v, _ := f.Int()
  42. return v
  43. }
  44. func (f StrTo) MustInt64() int64 {
  45. v, _ := f.Int64()
  46. return v
  47. }
  48. func (f StrTo) String() string {
  49. if f.Exist() {
  50. return string(f)
  51. }
  52. return ""
  53. }
  54. // Convert any type to string.
  55. func ToStr(value interface{}, args ...int) (s string) {
  56. switch v := value.(type) {
  57. case bool:
  58. s = strconv.FormatBool(v)
  59. case float32:
  60. s = strconv.FormatFloat(float64(v), 'f', argInt(args).Get(0, -1), argInt(args).Get(1, 32))
  61. case float64:
  62. s = strconv.FormatFloat(v, 'f', argInt(args).Get(0, -1), argInt(args).Get(1, 64))
  63. case int:
  64. s = strconv.FormatInt(int64(v), argInt(args).Get(0, 10))
  65. case int8:
  66. s = strconv.FormatInt(int64(v), argInt(args).Get(0, 10))
  67. case int16:
  68. s = strconv.FormatInt(int64(v), argInt(args).Get(0, 10))
  69. case int32:
  70. s = strconv.FormatInt(int64(v), argInt(args).Get(0, 10))
  71. case int64:
  72. s = strconv.FormatInt(v, argInt(args).Get(0, 10))
  73. case uint:
  74. s = strconv.FormatUint(uint64(v), argInt(args).Get(0, 10))
  75. case uint8:
  76. s = strconv.FormatUint(uint64(v), argInt(args).Get(0, 10))
  77. case uint16:
  78. s = strconv.FormatUint(uint64(v), argInt(args).Get(0, 10))
  79. case uint32:
  80. s = strconv.FormatUint(uint64(v), argInt(args).Get(0, 10))
  81. case uint64:
  82. s = strconv.FormatUint(v, argInt(args).Get(0, 10))
  83. case string:
  84. s = v
  85. case []byte:
  86. s = string(v)
  87. default:
  88. s = fmt.Sprintf("%v", v)
  89. }
  90. return s
  91. }
  92. type argInt []int
  93. func (a argInt) Get(i int, args ...int) (r int) {
  94. if i >= 0 && i < len(a) {
  95. r = a[i]
  96. } else if len(args) > 0 {
  97. r = args[0]
  98. }
  99. return
  100. }
  101. // HexStr2int converts hex format string to decimal number.
  102. func HexStr2int(hexStr string) (int, error) {
  103. num := 0
  104. length := len(hexStr)
  105. for i := 0; i < length; i++ {
  106. char := hexStr[length-i-1]
  107. factor := -1
  108. switch {
  109. case char >= '0' && char <= '9':
  110. factor = int(char) - '0'
  111. case char >= 'a' && char <= 'f':
  112. factor = int(char) - 'a' + 10
  113. default:
  114. return -1, fmt.Errorf("invalid hex: %s", string(char))
  115. }
  116. num += factor * PowInt(16, i)
  117. }
  118. return num, nil
  119. }
  120. // Int2HexStr converts decimal number to hex format string.
  121. func Int2HexStr(num int) (hex string) {
  122. if num == 0 {
  123. return "0"
  124. }
  125. for num > 0 {
  126. r := num % 16
  127. c := "?"
  128. if r >= 0 && r <= 9 {
  129. c = string(r + '0')
  130. } else {
  131. c = string(r + 'a' - 10)
  132. }
  133. hex = c + hex
  134. num = num / 16
  135. }
  136. return hex
  137. }