PHP的pack()函数在Go语言中的等价函数是什么?

huangapple go评论88阅读模式
英文:

Go equivalent for PHP's pack()?

问题

在Go语言中,要编码二进制数据(如整数、浮点数等),可以按照以下方式进行:

package main

import (
	"encoding/binary"
	"fmt"
)

func main() {
	uint32 := uint32(92301)
	uint16 := uint16(65535)
	float := float32(0.0012)

	uint32Bytes := make([]byte, 4)
	uint16Bytes := make([]byte, 2)
	floatBytes := make([]byte, 4)

	binary.LittleEndian.PutUint32(uint32Bytes, uint32)
	binary.LittleEndian.PutUint16(uint16Bytes, uint16)
	binary.LittleEndian.PutUint32(floatBytes, math.Float32bits(float))

	fmt.Printf("uint32: %x\n", uint32Bytes) // 8d680100
	fmt.Printf("uint16: %x\n", uint16Bytes) // ffff
	fmt.Printf("float: %x\n", floatBytes)   // 52499d3a
}

以上是将PHP代码转换为Go代码的示例。在Go中,我们使用encoding/binary包来进行二进制编码和解码。我们使用binary.LittleEndian来指定使用小端字节序。通过PutUint32PutUint16函数,我们将整数类型转换为字节切片。对于浮点数,我们使用math.Float32bits函数将其转换为无符号整数,然后再进行字节切片转换。最后,我们使用Printf函数打印结果。

请注意,Go语言中的变量名不能与关键字相同,因此我将变量名从float改为float32

英文:

In PHP, to encode binary data, such as integers, floats and so on, I'd do the following:

<?php

$uint32 = pack("V", 92301);
$uint16 = pack("v", 65535);
$float = pack("f", 0.0012);

echo "uint32: " . bin2hex($uint32) . "\n"; // 8d680100
echo "uint16: " . bin2hex($uint16) . "\n"; // ffff
echo "float: " . bin2hex($float) . "\n"; // 52499d3a

How can I bring this code into Go?

答案1

得分: 4

为什么在一种语言中,pack() 函数中的类型已经是该语言的本机类型,你还需要使用这样的函数呢?

要编码二进制数据,你可以使用 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())
}

playground

有了这个,编码其他数据结构就相当容易了。你只需要将 binary.Write 的第三个参数更改为你希望的数据类型,函数就会完成所有的魔法

英文:

Why would you need to use a function such as pack() in a language where the types in pack() are already native types of the language itself?

To encode binary data you'd use the package encoding/binary. To replicate your code:

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())
}

(playground)

With that, it's fairly easy to get going encoding other data structures. You really just need to change the third argument of binary.Write to be of the data type you wish, and the function will do all the magic!

答案2

得分: 0

这不是一个完整的答案,但是因为我自己也在寻找以下内容,所以我认为这也可以帮助其他人。

要实现与PHP的bin2hex()函数相同的功能,你可以这样做:

import "encoding/hex"

func bin2hex(str string) string {
    return hex.EncodeToString([]byte(str))
}
英文:

This is not a complete answer, but since I've been looking for the following myself, I thought it could help others here too.

For a direct equivalent of php's bin2hex(), you can do:

import "encoding/hex"

func bin2hex(str string) string {
  return hex.EncodeToString([]byte(str))
}

huangapple
  • 本文由 发表于 2016年2月29日 03:39:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/35687305.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定