2 回答

TA貢獻1856條經(jīng)驗 獲得超17個贊
為什么你需要使用一個函數(shù),比如pack()在其中類型pack()已經(jīng)是語言本身的本地類型的語言中?
要編碼二進制數(shù)據(jù),您將使用包encoding/binary. 要復制您的代碼:
package main
import (
"bytes"
"encoding/binary"
"fmt"
)
func main() {
buf := new(bytes.Buffer)
byteOrder := binary.LittleEndian
binary.Write(buf, byteOrder, uint32(92301))
fmt.Printf("uint32: %x\n", buf.Bytes())
buf.Reset()
binary.Write(buf, byteOrder, uint16(65535))
fmt.Printf("uint16: %x\n", buf.Bytes())
buf.Reset()
binary.Write(buf, byteOrder, float32(0.0012))
fmt.Printf("float: %x\n", buf.Bytes())
}
有了這個,開始編碼其他數(shù)據(jù)結構就相當容易了。您真的只需要將 的第三個參數(shù)更改為binary.Write您希望的數(shù)據(jù)類型,該函數(shù)將發(fā)揮所有作用!

TA貢獻1852條經(jīng)驗 獲得超7個贊
這不是一個完整的答案,但由于我自己一直在尋找以下內(nèi)容,我認為它也可以幫助其他人。
對于直接等效于 php 的 bin2hex(),您可以執(zhí)行以下操作:
import "encoding/hex"
func bin2hex(str string) string {
return hex.EncodeToString([]byte(str))
}
- 2 回答
- 0 關注
- 136 瀏覽
添加回答
舉報