binary.Read()在struct中不产生期望值

脉冲0ne:

我正在尝试制作一个MOBI文件解析器,并且试图通过binary.Read()将一些二进制文件解析为一个结构时遇到了一个问题。

我以为这是一个对齐问题,但是我为什么不得到期望值感到茫然。我已经通过libmobi运行.mobi文件来测试我的代码输出,并检查了.mobi的二进制文件,以验证我没有发疯并且libmobi代码没有做任何奇怪的事情(这是不)。

这是一个简化的示例:

package main

import (
    "bytes"
    "encoding/binary"
    "fmt"
)

type Header struct {
    Type        [4]byte
    Creator     [4]byte
    Uid         uint32
    Next        uint32
    RecordCount uint16
}

func main() {
    testBytes := []byte{66, 79, 79, 75, 77, 79, 66, 73, 0, 0, 1, 17, 0, 0, 0, 0, 0, 136}

    h := Header{}
    buf := bytes.NewBuffer(testBytes)
    binary.Read(buf, binary.LittleEndian, &h)

    fmt.Printf("%s\n", h.Type)    // BOOK, as expected
    fmt.Printf("%s\n", h.Creator) // MOBI, as expected
    fmt.Printf("%d\n", h.Next)    // 0, as expected

    fmt.Printf("%d\n", h.Uid)
    // expecting Uid to be 273, but it's 285278208...
    fmt.Printf("%d\n", h.RecordCount)
    // expecting RecordCount to be 136, but it's 34816...
}

任何帮助将不胜感激!

编辑:这是从xxdon 进行的十六进制字节book.mobi

424f 4f4b 4d4f 4249 0000 0111 0000 0000 0088

领先:

是的,BigEndian效果更好

package main

import (
    "bytes"
    "encoding/binary"
    "fmt"
)

type Header struct {
    Type        [4]byte
    Creator     [4]byte
    Uid         uint32
    Next        uint32
    RecordCount uint16
}

func main() {
    testBytes := []byte{66, 79, 79, 75, 77, 79, 66, 73, 0, 0, 1, 17, 0, 0, 0, 0, 0, 136}

    h := Header{}
    buf := bytes.NewBuffer(testBytes)
    binary.Read(buf, binary.BigEndian, &h)

    fmt.Printf("%s\n", h.Type)    // BOOK, as expected
    fmt.Printf("%s\n", h.Creator) // MOBI, as expected
    fmt.Printf("%d\n", h.Next)    // 0, as expected

    fmt.Printf("%d\n", h.Uid)
    // expecting Uid to be 273, but it's 285278208...
    fmt.Printf("%d\n", h.RecordCount)
    // expecting RecordCount to be 136, but it's 34816...
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章