1 Star 1 Fork 0

iceinto / ipkg

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
uint.go 1.86 KB
一键复制 编辑 原始数据 按行查看 历史
package iconv
import (
"gitee.com/iceinto/ipkg/ibinary"
"strconv"
)
// Uint converts `any` to uint.
func Uint(any interface{}) uint {
if any == nil {
return 0
}
if v, ok := any.(uint); ok {
return v
}
return uint(Uint64(any))
}
// Uint8 converts `any` to uint8.
func Uint8(any interface{}) uint8 {
if any == nil {
return 0
}
if v, ok := any.(uint8); ok {
return v
}
return uint8(Uint64(any))
}
// Uint16 converts `any` to uint16.
func Uint16(any interface{}) uint16 {
if any == nil {
return 0
}
if v, ok := any.(uint16); ok {
return v
}
return uint16(Uint64(any))
}
// Uint32 converts `any` to uint32.
func Uint32(any interface{}) uint32 {
if any == nil {
return 0
}
if v, ok := any.(uint32); ok {
return v
}
return uint32(Uint64(any))
}
// Uint64 converts `any` to uint64.
func Uint64(any interface{}) uint64 {
if any == nil {
return 0
}
switch value := any.(type) {
case int:
return uint64(value)
case int8:
return uint64(value)
case int16:
return uint64(value)
case int32:
return uint64(value)
case int64:
return uint64(value)
case uint:
return uint64(value)
case uint8:
return uint64(value)
case uint16:
return uint64(value)
case uint32:
return uint64(value)
case uint64:
return value
case float32:
return uint64(value)
case float64:
return uint64(value)
case bool:
if value {
return 1
}
return 0
case []byte:
return ibinary.DecodeToUint64(value)
default:
s := String(value)
// Hexadecimal
if len(s) > 2 && s[0] == '0' && (s[1] == 'x' || s[1] == 'X') {
if v, e := strconv.ParseUint(s[2:], 16, 64); e == nil {
return v
}
}
// Octal
if len(s) > 1 && s[0] == '0' {
if v, e := strconv.ParseUint(s[1:], 8, 64); e == nil {
return v
}
}
// Decimal
if v, e := strconv.ParseUint(s, 10, 64); e == nil {
return v
}
// Float64
return uint64(Float64(value))
}
}
1
https://gitee.com/iceinto/ipkg.git
git@gitee.com:iceinto/ipkg.git
iceinto
ipkg
ipkg
v1.0.2

搜索帮助