go大小端互转

package main

import (
	"encoding/binary"
	"fmt"
)

func main() {
	// 127.0.0.1主机字节序
	var value uint32 = 2130706433

	// 主机字节序->网络字节序
	bigData := make([]byte, 4)
	binary.LittleEndian.PutUint32(bigData, value)
	bigNum := binary.BigEndian.Uint32(bigData)
	fmt.Println(bigNum)

	// 网络字节序->主机字节序
	smallData := make([]byte, 4)
	binary.BigEndian.PutUint32(smallData, bigNum)
	smallNum := binary.LittleEndian.Uint32(smallData)
	fmt.Println(smallNum)
}

posted on 2024-10-01 09:43  王景迁  阅读(2)  评论(0编辑  收藏  举报

导航