1

我的代码有问题。我将偏移值放入列表框中,然后读取它并在文件中查找它。我使用以下代码。

    Dim bw As New BinaryWriter(File.Open(filename, FileMode.Open, FileAccess.ReadWrite))
    Try
        For j As Integer = 0 To ListBox2.Items.Count - 1
            czytana = ListBox2.Items.Item(j)
            tablica = czytana.Split(" ") ' czytana is in format OFFSET: BYTE BYTE, offset is a hex addr
            tablica(0) = tablica(0).Replace(":", "") 'I remove : from "OFFSET:"
            bw.BaseStream.Seek("&H" + tablica(0), SeekOrigin.Begin) ' in ex. I got tablica(0)=000CFDD6, and I want to get &HCFDD6, but what I get is &H000CFDD6
            'some part of code in here which does its job properly
        Next j
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
    bw.Close()

问题是:我需要使用 tablica(0) 作为偏移量,使用 tablica(1) 和 tablica(2) 作为字节。我想要做的是打开文件,选择偏移量并用 tablica(2) 替换。czytana 的格式为“tablica(0): tablica(1) tablica(2).

有人介意帮忙吗?:)

4

1 回答 1

0

根据您的评论,我了解到您有以下输入:

OFFSET -> tablica(0). Sample Value: 000CFDD6
BYTE to write from OFFSET -> tablica(2). Sample Value: 6F

您的代码应如下所示:

bw.BaseStream.Seek(curOffset, SeekOrigin.Begin)
bw.Write(curByte)

在哪里:

Dim curOffset As Long = Long.Parse(tablica(0), System.Globalization.NumberStyles.HexNumber)
'An equivalent approach would be: Dim offset As Long = Int64.Parse("&H" & tablica(0))
'Where all the zeroes after &H (and before the first non-zero character) do not matter; &H0001 is the same than &H1
Dim curByte as Byte = Byte.Parse(Integer.Parse(tablica(2), System.Globalization.NumberStyles.HexNumber))
于 2013-08-12T09:37:13.930 回答