goProject/trunk/goutil/intAndBytesUtil/int.go

51 lines
1.2 KiB
Go
Raw Normal View History

2025-01-06 16:01:02 +08:00
package intAndBytesUtil
import (
"bytes"
"encoding/binary"
)
// 整形转换成字节(无效,因为系统无法判断读取的字节数)
// nint型数字
// order大、小端的枚举
// 返回值:对应的字节数组
// func IntToBytes(n int, order binary.ByteOrder) []byte {
// bytesBuffer := bytes.NewBuffer([]byte{})
// binary.Write(bytesBuffer, order, n)
// return bytesBuffer.Bytes()
// }
// 整形转换成字节
// nint16型数字
// order大、小端的枚举
// 返回值:对应的字节数组
func Int16ToBytes(n int16, order binary.ByteOrder) []byte {
bytesBuffer := bytes.NewBuffer([]byte{})
binary.Write(bytesBuffer, order, n)
return bytesBuffer.Bytes()
}
// 整形转换成字节
// nint32型数字
// order大、小端的枚举
// 返回值:对应的字节数组
func Int32ToBytes(n int32, order binary.ByteOrder) []byte {
bytesBuffer := bytes.NewBuffer([]byte{})
binary.Write(bytesBuffer, order, n)
return bytesBuffer.Bytes()
}
// 整形转换成字节
// nint64型数字
// order大、小端的枚举
// 返回值:对应的字节数组
func Int64ToBytes(n int64, order binary.ByteOrder) []byte {
bytesBuffer := bytes.NewBuffer([]byte{})
binary.Write(bytesBuffer, order, n)
return bytesBuffer.Bytes()
}