Ai
43 Star 150 Fork 49

梁大帅/mqant

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
params_bytes.go 2.37 KB
一键复制 编辑 原始数据 按行查看 历史
liangdas 提交于 2017-09-30 10:47 +08:00 . =gofmt
// Copyright 2014 loolgame Author. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package utils
import (
"encoding/binary"
"encoding/json"
"math"
)
func BoolToBytes(v bool) []byte {
var buf = make([]byte, 1)
if v {
buf[0] = 1
} else {
buf[0] = 0
}
return buf
}
func BytesToBool(buf []byte) bool {
var data bool = buf[0] != 0
return data
}
func Int32ToBytes(i int32) []byte {
var buf = make([]byte, 4)
binary.BigEndian.PutUint32(buf, uint32(i))
return buf
}
func BytesToInt32(buf []byte) int32 {
return int32(binary.BigEndian.Uint32(buf))
}
func Int64ToBytes(i int64) []byte {
var buf = make([]byte, 8)
binary.BigEndian.PutUint64(buf, uint64(i))
return buf
}
func BytesToInt64(buf []byte) int64 {
return int64(binary.BigEndian.Uint64(buf))
}
func Float32ToBytes(float float32) []byte {
bits := math.Float32bits(float)
bytes := make([]byte, 4)
binary.LittleEndian.PutUint32(bytes, bits)
return bytes
}
func BytesToFloat32(bytes []byte) float32 {
bits := binary.LittleEndian.Uint32(bytes)
return math.Float32frombits(bits)
}
func Float64ToBytes(float float64) []byte {
bits := math.Float64bits(float)
bytes := make([]byte, 8)
binary.LittleEndian.PutUint64(bytes, bits)
return bytes
}
func BytesToFloat64(bytes []byte) float64 {
bits := binary.LittleEndian.Uint64(bytes)
return math.Float64frombits(bits)
}
func MapToBytes(jmap map[string]interface{}) ([]byte, error) {
bytes, err := json.Marshal(jmap)
return bytes, err
}
func BytesToMap(bytes []byte) (map[string]interface{}, error) {
v := make(map[string]interface{})
err := json.Unmarshal(bytes, &v)
return v, err
}
func MapToBytesString(jmap map[string]string) ([]byte, error) {
bytes, err := json.Marshal(jmap)
return bytes, err
}
func BytesToMapString(bytes []byte) (map[string]string, error) {
v := make(map[string]string)
err := json.Unmarshal(bytes, &v)
return v, err
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/liangdas/mqant.git
git@gitee.com:liangdas/mqant.git
liangdas
mqant
mqant
v1.6.7

搜索帮助