将二进制数据写入文件(从十六进制转储)

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

Writing binary data to a file (from hexdump)

问题

我尝试在Go中做一些非常简单的事情,但我找不到任何资源。

我收到一个十六进制转储,并且我想将其写入文件,但是两个文件(源文件和目标文件)的内容根本不匹配。目前我找到的唯一方法是手动在每两个字符之间添加\x

我尝试循环遍历我的字符串并添加\x,字符串看起来相同,但输出非常不同。

这段代码手动工作:

binary.Write(f, binary.LittleEndian, []byte("\x00\x00\x00\x04\x0A\xFA\x64\xA7\x00\x03\x31\x30"))

但是我无法从字符串"000000040afa64a700033130"中实现相同的效果...

这是我目前的做法(这是我在Python3中的做法):

text := "000000040afa64a700033130"                                                                     
j := 0                                    
f, _ := os.OpenFile("gotest", os.O_WRONLY|os.O_CREATE, 0600)                                    
for i := 0; i < len(text); i += 2 {                                    
    if (i + 2) <= len(text) {                                    
        j = i + 2                                    
    }                                                                                                       
    value, _ := strconv.ParseInt(hex, 16, 8)
    binary.Write(f, binary.LittleEndian,value)                                    
    s = append(s, value)                                                                     
}
英文:

I try to do something very simple in Go but I do not manage to find any resources.

I receive an hexadump and I want to write it to a file but the content of both files (src and dst) do not match at all. Currently the only way I have find it's to manually add \x every 2 characters.

I tried to loop over my string and add \x the string looks identical but output is very different.

This code manually works:

binary.Write(f, binary.LittleEndian, []byte(&quot;\x00\x00\x00\x04\x0A\xFA\x64\xA7\x00\x03\x31\x30&quot;))

But I did not manage to make it from string "000000040afa64a700033130"...

What i currently do (this is what I do in python3):

text := &quot;000000040afa64a700033130&quot;                                                                     
j := 0                                    
f, _ := os.OpenFile(&quot;gotest&quot;, os.O_WRONLY|os.O_CREATE, 0600)                                    
for i := 0; i &lt; len(text); i += 2 {                                    
    if (i + 2) &lt;= len(text) {                                    
        j = i + 2                                    
    }                                                                                                       
    value, _ := strconv.ParseInt(hex, 16, 8)
    binary.Write(f, binary.LittleEndian,value)                                    
    s = append(s, value)                                                                     
}  

答案1

得分: 1

如果您的十六进制数据以字符串形式存在,并且您想要写入原始字节,您需要先进行转换,更简单的方法是使用hex.Decode

import (
    "encoding/hex"
    "io/ioutil"
)

func foo() {
    stringData := []byte("48656c6c6f20476f7068657221")
    hexData := make([]byte, hex.DecodedLen(len(stringData)))
    
    _, err := hex.Decode(stringData, hexData)
    // 处理错误

    err := ioutil.WriteFile("filename", hexData, 0644)
    // 处理错误
}

根据您的使用情况,您可以切换到使用ioutil.WriteFile。它将给定的字节切片写入文件,如果文件不存在,则创建该文件,如果文件已存在,则将其截断。

英文:

If your hex data is in the from of a string and you want to write the raw bytes you'll have to convert it first, the easier way would be to use hex.Decode.

import (
    &quot;encoding/hex&quot;
    &quot;io/ioutil&quot;
)

func foo() {
    stringData := []byte(&quot;48656c6c6f20476f7068657221&quot;)
    hexData := make([]byte, hex.DecodedLen(len(stringData)))
    
    _, err := hex.Decode(stringData, hexData)
    // handle err

    err := ioutil.WriteFile(&quot;filename&quot;, hexData, 0644)
    // handle err
}

Based on your use you could swap over to using ioutil.WriteFile. It writes the given byte slice to a file, creating the file if it doesn't exist or truncating it in the case it already exists.

huangapple
  • 本文由 发表于 2021年7月26日 22:36:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/68531805.html
匿名

发表评论

匿名网友

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

确定