代码拉取完成,页面将自动刷新
package utilsConvert
import (
"bytes"
"encoding/binary"
"reflect"
"strconv"
)
// ToInt 转int
func ToInt(src interface{}) int {
var dst int
val := reflect.ValueOf(src)
switch src.(type) {
case int, int8, int16, int32, int64:
dst = int(val.Int())
case uint, uint8, uint16, uint32, uint64:
dst = int(val.Uint())
case float32, float64:
dst = int(val.Float())
case string:
if len(val.String()) == 0 {
return 0
}
dst, _ = strconv.Atoi(val.String())
default:
dst = int(-1)
}
return dst
}
// ToInt64 转int64
func ToInt64(src interface{}) int64 {
var dst int64
val := reflect.ValueOf(src)
switch src.(type) {
case int, int8, int16, int32, int64:
dst = val.Int()
case uint, uint8, uint16, uint32, uint64:
dst = int64(val.Uint())
case float32, float64:
dst = int64(val.Float())
case string:
if len(val.String()) == 0 {
return int64(0)
}
dst, _ = strconv.ParseInt(val.String(), 10, 64)
default:
dst = int64(-1)
}
return dst
}
// ToFloat64 转float64
func ToFloat64(src interface{}) float64 {
var dst float64
val := reflect.ValueOf(src)
switch src.(type) {
case int, int8, int16, int32, int64:
dst = float64(val.Int())
case uint, uint8, uint16, uint32, uint64:
dst = float64(val.Uint())
case float64, float32:
dst = val.Float()
case string:
if len(val.String()) == 0 {
return float64(0)
}
dst, _ = strconv.ParseFloat(val.String(), 64)
default:
dst = float64(-1)
}
return dst
}
func IntToBitBytes(n int, length int) []byte {
switch length {
case 32:
data := int32(n)
bytebuf := bytes.NewBuffer([]byte{})
binary.Write(bytebuf, binary.BigEndian, data)
return bytebuf.Bytes()
case 64:
data := int64(n)
bytebuf := bytes.NewBuffer([]byte{})
binary.Write(bytebuf, binary.BigEndian, data)
return bytebuf.Bytes()
default:
data := int32(n)
bytebuf := bytes.NewBuffer([]byte{})
binary.Write(bytebuf, binary.BigEndian, data)
return bytebuf.Bytes()
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。