从 x pos 二进制文件中提取文本

我正在尝试读取二进制文件以从不同位置获取字符串;读取 {IP,login,pwd} 我的想法是找到第一个 ip 并在之后读取数据,因为之间的长度是一样的:


Offset(h) 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F

00000060  5F 00 00 00 00 11 E0 BB 5F 00 00 00 00 01 34 31  _.....à»_.....40

00000070  2E 31 39 31 2E 39 37 2E 36 32 00 00 00 00 00 00  .091.17.02......

00000080  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................

00000090  00 00 73 75 70 70 6F 72 74 00 00 00 00 00 00 00  ..support.......

000000A0  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................

000000B0  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................

000000C0  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................

000000D0  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................

000000E0  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................

000000F0  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................

00000100  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................

00000110  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................

00000120  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................

00000130  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................

00000140  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................

00000150  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................

00000160  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................

00000170  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................

我的围棋代码


import (

    "io"

    "log"

    "os"

)

func main() {

    file, err := os.Open("data")

    if err != nil {

        log.Fatal(err)

    }

    defer file.Close()

    o2, err := file.Seek(110, io.SeekCurrent) <---- find first occurrence 

    byteSlice := make([]byte, 32)


    bytesRead, err := file.Read(byteSlice)

    if err != nil {

        log.Fatal(err)

    }

    

    log.Printf("IP: %s\n", byteSlice)

    

}

如何在找到 ip 后找到所有出现的 { ip, login, pwd },用我的代码我只能找到第一个 (ip) 32 位。



qq_花开花谢_0
浏览 97回答 2
2回答

皈依舞

谢谢你的帮助,我使用 file.Seek(x,0) 像这样的代码:file, err := os.Open("data.bin")check(err)defer file.Close()o1, err := file.Seek(110, 0)&nbsp; <--- first positioncheck(err)_ip := make([]byte, 15)ipRead, err := file.Read(_ip)clean_ip := strings.ReplaceAll(string(_ip[:]), " ", " ")o2, err := file.Seek(21, io.SeekCurrent)check(err)_user := make([]byte, 15)userRead, err := file.Read(_user)clean_user := strings.ReplaceAll(string(_user[:]), " ", " ")o3, err := file.Seek(532, 0)&nbsp; <---- Second positioncheck(err)_pwd := make([]byte, 20)pwdRead, err := file.Read(_pwd)clean_pwd := strings.ReplaceAll(string(_pwd[:]), " ", " ")它的工作但不干净

繁花如伊

一种方法是迭代文件。本质上,您可以重新实现该strings程序。它类似于使用bufio#Scanner.Scan,但您需要迭代空字节而不是换行符。您可以使用 bufio#Scanner.Split,Go 甚至提供了一些预制功能。我采用了ScanLines并修改为使用空字节而不是换行符。此外,也 ScanLines返回空行,所以我添加了一个空文本检查:package mainimport (&nbsp; &nbsp;"bufio"&nbsp; &nbsp;"strings")func null(b []byte, eof bool) (int, []byte, error) {&nbsp; &nbsp;if eof { return 0, nil, nil }&nbsp; &nbsp;for n := range b {&nbsp; &nbsp; &nbsp; if b[n] == 0 {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return n+1, b[:n], nil&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp;}&nbsp; &nbsp;return len(b), b, nil}func main() {&nbsp; &nbsp;s := bufio.NewScanner(strings.NewReader("March\x00April\x00May\x00\x00June"))&nbsp; &nbsp;s.Split(null)&nbsp; &nbsp;for s.Scan() {&nbsp; &nbsp; &nbsp; if s.Text() != "" {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;println(s.Text())&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp;}}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go