英文:
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
来指定使用小端字节序。通过PutUint32
和PutUint16
函数,我们将整数类型转换为字节切片。对于浮点数,我们使用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())
}
有了这个,编码其他数据结构就相当容易了。你只需要将 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())
}
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))
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论