1 Star 0 Fork 1

技术狼/go-fun

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
byte.go 1.58 KB
一键复制 编辑 原始数据 按行查看 历史
技术狼 提交于 1年前 . no message
package fun
import (
"encoding/base32"
"encoding/binary"
"io"
"math"
"reflect"
"unsafe"
)
// @title: []byte转float32
// @param: []byte
// @return: float32
// @description:
// @date: 2024/6/11 22:32
func BytesToFloat32(bytes []byte) float32 {
if len(bytes) == 0 {
return 0
}
bits := binary.LittleEndian.Uint32(bytes)
return math.Float32frombits(bits)
}
// @title: []byte转float64
// @param: []byte
// @return: float64
// @description:
// @date: 2024/6/11 22:32
func BytesToFloat64(bytes []byte) float64 {
bits := binary.LittleEndian.Uint64(bytes)
return math.Float64frombits(bits)
}
// @title: 将字节切片转换为字符串
// @param: []byte
// @return: string
// @description: 不安全的[]byte转string
// @date: 2024/6/11 22:32
func UnsafeBytesToString(bytes []byte) string {
if len(bytes) == 0 {
return ""
}
hdr := &reflect.StringHeader{
Data: uintptr(unsafe.Pointer(&bytes[0])),
Len: len(bytes),
}
return *(*string)(unsafe.Pointer(hdr))
}
// @title: []byte转io.Reader
// @param: []byte
// @return: io.Reader
// @description: 接受一个字节切片 []byte 并将其转换为 io.Reader
// @date: 2024/6/11 22:32
func BytesToIoReader(b []byte) io.Reader {
return IoReader(b)
}
// @title: base32加密
// @param: []byte
// @return: string
// @description:
// @date: 2024/6/11 22:32
func Base32Encode(src []byte) string {
return base32.StdEncoding.EncodeToString(src)
}
// @title: base32解密
// @param: []byte
// @return: []byte, error
// @description:
// @date: 2024/6/11 22:32
func Base32Decode(s string) ([]byte, error) {
return base32.StdEncoding.DecodeString(s)
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/jishulangcom/go-fun.git
git@gitee.com:jishulangcom/go-fun.git
jishulangcom
go-fun
go-fun
v0.0.4

搜索帮助