handle_typechange.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package mokuai
  2. import (
  3. "encoding/binary"
  4. "io/ioutil"
  5. "math"
  6. "net/http"
  7. "strconv"
  8. )
  9. // 整数型转文本型(int)
  10. func Type_IntToString(i int) string {
  11. return strconv.Itoa(i)
  12. }
  13. // 文本型转整数型(int)
  14. func Type_StringToInt(str string) int {
  15. i, _ := strconv.Atoi(str)
  16. return i
  17. }
  18. // string到int64
  19. func Type_StringToInt64(str string) int64 {
  20. i64, _ := strconv.ParseInt(str, 10, 64)
  21. return i64
  22. }
  23. // int64到string
  24. func Type_Int64ToString(i64 int64) string {
  25. str := strconv.FormatInt(i64, 10)
  26. return str
  27. }
  28. // int到int64
  29. func Type_IntToInt64(i int) int64 {
  30. return int64(i)
  31. }
  32. // int64到int
  33. func Type_Int64ToInt(i64 int64) int {
  34. return int(i64)
  35. }
  36. /* // float(32)到string
  37. func Type_FloatToString(f float32) string {
  38. return strconv.FormatFloat(f,'E',-1,32)
  39. }*/
  40. // float(64)到string
  41. func Type_Float64ToString(f64 float64) string {
  42. return strconv.FormatFloat(f64, 'E', -1, 64)
  43. }
  44. /* //string到float(32)
  45. func Type_StringToFloat(str string) float32 {
  46. f , _ := strconv.ParseFloat(str,32)
  47. return f
  48. }*/
  49. // string到float(64)
  50. func Type_StringToFloat64(str string) float64 {
  51. f64, _ := strconv.ParseFloat(str, 64)
  52. return f64
  53. }
  54. // float(32)到Byte
  55. func Type_FloatToByte(float float32) []byte {
  56. bits := math.Float32bits(float)
  57. bytes := make([]byte, 4)
  58. binary.LittleEndian.PutUint32(bytes, bits)
  59. return bytes
  60. }
  61. // Byte到float(32)
  62. func Type_ByteToFloat(bytes []byte) float32 {
  63. bits := binary.LittleEndian.Uint32(bytes)
  64. return math.Float32frombits(bits)
  65. }
  66. // float(64)到Byte
  67. func Type_Float64ToByte(float float64) []byte {
  68. bits := math.Float64bits(float)
  69. bytes := make([]byte, 8)
  70. binary.LittleEndian.PutUint64(bytes, bits)
  71. return bytes
  72. }
  73. // Byte到float(64)
  74. func Type_ByteToFloat64(bytes []byte) float64 {
  75. bits := binary.LittleEndian.Uint64(bytes)
  76. return math.Float64frombits(bits)
  77. }
  78. // string 转 []byte
  79. func Type_StringToByte(str string) []byte {
  80. return []byte(str)
  81. }
  82. // []byte 转 string
  83. func Type_ByteToString(byteval []byte) string {
  84. return string(byteval)
  85. }
  86. // string 转 rune
  87. func Type_StringToRune(str string) []rune {
  88. return []rune(str)
  89. }
  90. // rune 转 string
  91. func Type_RuneToString(runeval []rune) string {
  92. return string(runeval)
  93. }
  94. // Response 转 string
  95. func Type_ResponseToString(resp http.Response) string {
  96. bodyRes, _ := ioutil.ReadAll(resp.Body)
  97. return string(bodyRes)
  98. }
  99. // Response 转 []byte
  100. func Type_ResponseToByte(resp http.Response) []byte {
  101. bodyRes, _ := ioutil.ReadAll(resp.Body)
  102. return bodyRes
  103. }