2 Star 1 Fork 0

法马智慧/fmgo

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
bytex.go 8.30 KB
一键复制 编辑 原始数据 按行查看 历史
Asdybing 提交于 2022-03-11 14:29 . no commit message
package bytex
import (
"bytes"
"encoding/binary"
"fmt"
"gitee.com/fmpt/fmgo/logx"
"github.com/imroc/biu"
"strconv"
"strings"
)
func Int64ToBytes(i int64) []byte {
var buf = make([]byte, 8)
binary.BigEndian.PutUint64(buf, uint64(i))
return buf
}
func Int32ToBytes(i int32) []byte {
var buf = make([]byte, 4)
binary.BigEndian.PutUint32(buf, uint32(i))
return buf
}
func Int64ToBytesLe(i int64) []byte {
var buf = make([]byte, 8)
binary.LittleEndian.PutUint64(buf, uint64(i))
return buf
}
func Int32ToBytesLe(i int32) []byte {
var buf = make([]byte, 4)
binary.LittleEndian.PutUint32(buf, uint32(i))
return buf
}
func BytesToInt64(buf []byte) int64 {
return int64(binary.BigEndian.Uint64(buf))
}
//isSymbol表示有无符号
func BytesToInt(b []byte, isSymbol bool) (int, error) {
if isSymbol {
return bytesToIntS(b)
}
return bytesToIntU(b)
}
func BytesToIntLittleEndian(b []byte) (int, error) {
if len(b) == 3 {
b = append([]byte{0}, b...)
}
bytesBuffer := bytes.NewBuffer(b)
switch len(b) {
case 1:
var tmp int8
err := binary.Read(bytesBuffer, binary.LittleEndian, &tmp)
return int(tmp), err
case 2:
var tmp int16
err := binary.Read(bytesBuffer, binary.LittleEndian, &tmp)
return int(tmp), err
case 4:
var tmp int32
err := binary.Read(bytesBuffer, binary.LittleEndian, &tmp)
return int(tmp), err
default:
return 0, fmt.Errorf("%s", "BytesToIntLittleEndian bytes lenth is invaild!")
}
}
//字节数(大端)组转成int(无符号的)
func bytesToIntU(b []byte) (int, error) {
if len(b) == 3 {
b = append([]byte{0}, b...)
}
bytesBuffer := bytes.NewBuffer(b)
switch len(b) {
case 1:
var tmp uint8
err := binary.Read(bytesBuffer, binary.BigEndian, &tmp)
return int(tmp), err
case 2:
var tmp uint16
err := binary.Read(bytesBuffer, binary.BigEndian, &tmp)
return int(tmp), err
case 4:
var tmp uint32
err := binary.Read(bytesBuffer, binary.BigEndian, &tmp)
return int(tmp), err
default:
return 0, fmt.Errorf("%s", "BytesToInt bytes lenth is invaild!")
}
}
//字节数(大端)组转成int(有符号)
func bytesToIntS(b []byte) (int, error) {
if len(b) == 3 {
b = append([]byte{0}, b...)
}
bytesBuffer := bytes.NewBuffer(b)
switch len(b) {
case 1:
var tmp int8
err := binary.Read(bytesBuffer, binary.BigEndian, &tmp)
return int(tmp), err
case 2:
var tmp int16
err := binary.Read(bytesBuffer, binary.BigEndian, &tmp)
return int(tmp), err
case 4:
var tmp int32
err := binary.Read(bytesBuffer, binary.BigEndian, &tmp)
return int(tmp), err
default:
return 0, fmt.Errorf("%s", "BytesToInt bytes lenth is invaild!")
}
}
//整形转换成字节
func IntToBytes(n int, b byte) ([]byte, error) {
switch b {
case 1:
tmp := int8(n)
bytesBuffer := bytes.NewBuffer([]byte{})
_ = binary.Write(bytesBuffer, binary.BigEndian, &tmp)
return bytesBuffer.Bytes(), nil
case 2:
tmp := int16(n)
bytesBuffer := bytes.NewBuffer([]byte{})
_ = binary.Write(bytesBuffer, binary.BigEndian, &tmp)
return bytesBuffer.Bytes(), nil
case 3, 4:
tmp := int32(n)
bytesBuffer := bytes.NewBuffer([]byte{})
_ = binary.Write(bytesBuffer, binary.BigEndian, &tmp)
return bytesBuffer.Bytes(), nil
}
return nil, fmt.Errorf("IntToBytes b param is invaild")
}
func IntToBytesLe(n int, b byte) ([]byte, error) {
switch b {
case 1:
tmp := int8(n)
bytesBuffer := bytes.NewBuffer([]byte{})
_ = binary.Write(bytesBuffer, binary.LittleEndian, &tmp)
return bytesBuffer.Bytes(), nil
case 2:
tmp := int16(n)
bytesBuffer := bytes.NewBuffer([]byte{})
_ = binary.Write(bytesBuffer, binary.LittleEndian, &tmp)
return bytesBuffer.Bytes(), nil
case 3, 4:
tmp := int32(n)
bytesBuffer := bytes.NewBuffer([]byte{})
_ = binary.Write(bytesBuffer, binary.LittleEndian, &tmp)
return bytesBuffer.Bytes(), nil
}
return nil, fmt.Errorf("IntToBytesLe b param is invaild")
}
func ByteToBitStr(b byte) string {
//bitStr := biu.ByteToBinaryString(data)
//bitStrArr := strings.Split(bitStr, "")
//bitStr = strings.Join(bitStrArr, ",")
bitStr1 := strconv.Itoa(int((b >> 0) & 0x1))
bitStr2 := strconv.Itoa(int((b >> 1) & 0x1))
bitStr3 := strconv.Itoa(int((b >> 2) & 0x1))
bitStr4 := strconv.Itoa(int((b >> 3) & 0x1))
bitStr5 := strconv.Itoa(int((b >> 4) & 0x1))
bitStr6 := strconv.Itoa(int((b >> 5) & 0x1))
bitStr7 := strconv.Itoa(int((b >> 6) & 0x1))
bitStr8 := strconv.Itoa(int((b >> 7) & 0x1))
bitStr := bitStr1 + "," + bitStr2 + "," + bitStr3 + "," + bitStr4 + "," + bitStr5 + "," + bitStr6 + "," + bitStr7 + "," + bitStr8
return bitStr
}
func BytesToBitStr(datas []byte) string {
bitStr := biu.BytesToBinaryString(datas)
bitStr = strings.Replace(bitStr, " ", "", -1)
bitStrArr := strings.Split(bitStr, "")
bitStr = strings.Join(bitStrArr, ",")
bitStr = strings.TrimLeft(bitStr, "[,")
bitStr = strings.TrimRight(bitStr, "]")
return bitStr
}
func BitStrToBytes(data string) []byte {
bitStrArr := strings.Split(data, ",")
bitStr := strings.Join(bitStrArr, "")
bytes := biu.BinaryStringToBytes(bitStr)
return bytes
}
func PasstrafficIntToBit(datas []byte) string {
bitStr := biu.BytesToBinaryString(datas)
tempStr := strings.Replace(bitStr, " ", "", -1)
tempStr = strings.TrimLeft(tempStr, "[")
tempStr = strings.TrimRight(tempStr, "]")
strLen := len(tempStr)
// fix 0
if strLen < 32 {
tempStr = getFixedZeroString(32-strLen) + tempStr
}
// revert byte group every 8 bits
// WARNING: substring end index is not from 0
tempstr1 := Substring(tempStr, 0, 8)
tempstr2 := Substring(tempStr, 8, 16)
tempstr3 := Substring(tempStr, 16, 24)
tempstr4 := Substring(tempStr, 24, 32)
tempStr = tempstr4 + tempstr3 + tempstr2 + tempstr1
bitStrArr := strings.Split(tempStr, "")
tempStr = strings.Join(bitStrArr, ",")
tempStr += ","
return tempStr
}
func PasstrafficBitStrToInt(bitstr string) int {
var tempstr = bitstr
tempstr = strings.ReplaceAll(tempstr, " ", "")
tempstr = strings.ReplaceAll(tempstr, ",", "")
if len(tempstr) != 32 {
logx.Error("passtrafficBitStrToInt failed, invalid string length!")
return 0
}
// revert byte group every 8 bits
var tempstr1 = tempstr[0:8]
var tempstr2 = tempstr[8:16]
var tempstr3 = tempstr[16:24]
var tempstr4 = tempstr[24:32]
tempstr = tempstr4 + tempstr3 + tempstr2 + tempstr1
tempInt, err := BytesToInt(BitStrToBytes(tempstr), false)
if err != nil {
logx.Error(err)
}
return tempInt
}
func ReverseBits(num uint32) uint32 {
var res uint32
for i := 0; i < 32; i++ {
res = (res << 1) + (num & 1)
num = num >> 1
}
return res
}
func getFixedZeroString(cnt int) string {
var sb bytes.Buffer
for i := 0; i < cnt; i++ {
sb.WriteString("0")
}
return sb.String()
}
func OneArrayToTwoArray(bytes []byte, row int, column int) [][]byte {
var result [][]byte
for i := 0; i < row; i++ {
tmp := make([]byte, column)
result = append(result, tmp)
}
for i := 0; i < len(bytes); i++ {
result[i/column][i%column] = bytes[i]
}
return result
}
func Substring(str string, start int, end int) string {
rs := []rune(str)
length := len(rs)
if start < 0 || start > length {
panic("start is wrong")
}
if end < 0 || end > length {
panic("end is wrong")
}
return string(rs[start:end])
}
func IntToByteArray4(i int) []byte {
result := make([]byte, 4)
// 由高位到低位
result[0] = (byte)((i >> 24) & 0xFF)
result[1] = (byte)((i >> 16) & 0xFF)
result[2] = (byte)((i >> 8) & 0xFF)
result[3] = (byte)(i & 0xFF)
return result
}
func IntToByteArray2(i int) []byte {
result := make([]byte, 2)
// 由高位到低位
result[0] = (byte)((i >> 8) & 0xFF)
result[1] = (byte)(i & 0xFF)
return result
}
/*
* byte[]转int
* @param bytes
* @return
*/
func ByteArrayToInt(bytes []byte) int {
value := 0
// 由高位到低位
for i := 0; i < 4; i++ {
shift := (3 - i) * 8
//fmt.Println("bytes[i]:", bytes[i]&0xFF, "(bytes[i] & 0xFF) << shift ", int(bytes[i]&0xFF)<<shift, " shift:", shift)
value += int(bytes[i]&0xFF) << shift // 往高位游
}
//fmt.Println("值:",value)
return value
}
func Uint16ToBytes(n uint16) [2]byte {
return [2]byte{
byte(n >> 8),
byte(n),
}
}
func ReverseBytes(bytes *[]byte) {
for i, j := 0, len(*bytes)-1; i < j; i, j = i+1, j-1 {
(*bytes)[i], (*bytes)[j] = (*bytes)[j], (*bytes)[i]
}
}
func AppendZero(str string, _len int, before bool) []byte {
b := []byte(str)
if len(b) < _len {
l := _len - len(b)
ap := make([]byte, l)
if before {
return append(ap, b...)
}
return append(b, ap...)
}
return b
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/fmpt/fmgo.git
git@gitee.com:fmpt/fmgo.git
fmpt
fmgo
fmgo
v1.2.52

搜索帮助