简介
[N]byte[]byte
[]byteBytes
type Bytes []byte
字节数组与字符串

Bytes 转 string

b := Bytes{232, 191, 153, 230, 152, 175, 228, 184, 128, 228, 184, 170, 229, 173, 151, 231, 172, 166, 228, 184, 178}
s := string(b)
fmt.Println(s)

string 转 Bytes

s := "这是一个字符串"
b := Bytes(s)
fmt.Println(b)
字节数组与整数、浮点数

int16, uint16, int32, uint32, int64, uint64, float32, float64 转 Bytes

binaryWrite
binaryWrite
func Write(w io.Writer, order ByteOrder, data interface{}) error
io.WriterBytes
io.Writer
type Writer interface {
	Write(p []byte) (n int, err error)
}
BytesWrite(p []byte) (n int, err error)
func (b *Bytes) Write(p []byte) (n int, err error)  {
	*b = append(*b, p...)
	return len(p), nil
}
Bytesio.Writer

我们定义一个通用的转字节数组的函数


func GetBytes(order binary.ByteOrder, data interface{}) Bytes {
	b := Bytes{}
	_ = binary.Write(&b, order, data)
	return b
}
func main() {
	var i16 int16 = 0x1234
	var u16 uint16 = 0x5678
	var i32 int32 = 0x12345678
	var u32 uint32 = 0x87654321
	var i64 int64 = 0x1234567887654321
	var u64 uint64 = 0x8765432112345678
	var f32 float32 = 1234.5678
	var f64 float64 = 12345.67890
	/// 小端模式
	fmt.Println(GetBytes(binary.LittleEndian, i16))
	fmt.Println(GetBytes(binary.LittleEndian, u16))
	fmt.Println(GetBytes(binary.LittleEndian, i32))
	fmt.Println(GetBytes(binary.LittleEndian, u32))
	fmt.Println(GetBytes(binary.LittleEndian, i64))
	fmt.Println(GetBytes(binary.LittleEndian, u64))
	fmt.Println(GetBytes(binary.LittleEndian, f32))
	fmt.Println(GetBytes(binary.LittleEndian, f64))
	
	/// 大端模式
	fmt.Println(GetBytes(binary.BigEndian, i16))
	fmt.Println(GetBytes(binary.BigEndian, u16))
	fmt.Println(GetBytes(binary.BigEndian, i32))
	fmt.Println(GetBytes(binary.BigEndian, u32))
	fmt.Println(GetBytes(binary.BigEndian, i64))
	fmt.Println(GetBytes(binary.BigEndian, u64))
	fmt.Println(GetBytes(binary.BigEndian, f32))
	fmt.Println(GetBytes(binary.BigEndian, f64))
}

这样我们的整数、浮点数都转换成对应的字节数组了

Bytes 转 int16, uint16, int32, uint32, int64, uint64, float32, float64

Bytesuint16uint32uint64
binary.LittleEndianbinary.BigEndian
func main() {
	bytes := Bytes{1, 2, 3, 4, 5, 6, 7, 8}
	/// 小端模式
	fmt.Println(binary.LittleEndian.Uint16(bytes))
	fmt.Println(binary.LittleEndian.Uint32(bytes))
	fmt.Println(binary.LittleEndian.Uint64(bytes))
	/// 大端模式
	fmt.Println(binary.BigEndian.Uint16(bytes))
	fmt.Println(binary.BigEndian.Uint32(bytes))
	fmt.Println(binary.BigEndian.Uint64(bytes))
}
uint64int64
Bytesfloat32float64mathFloat32frombitsFloat64frombits
func main() {
	bytes := Bytes{1, 2, 3, 4, 5, 6, 7, 8}
	
	u32le := binary.LittleEndian.Uint32(bytes)
	u32be := binary.BigEndian.Uint32(bytes)	
	u64le := binary.LittleEndian.Uint64(bytes)
	u64be := binary.BigEndian.Uint64(bytes)
	
	/// 小端模式
	fmt.Println(math.Float32frombits(u32le))
	fmt.Println(math.Float64frombits(u64le))
	
	/// 大端模式
	fmt.Println(math.Float32frombits(u32be))
	fmt.Println(math.Float64frombits(u64be))
}
以上为本人新学习到的与字节数组相关的各种转换操作。如果后续有更好的也会更新上来。当然如过读者大人有更好的方式,欢迎评论区讨论学习 *^_^*