解析C#样式的十六进制字符串

时间:2013-08-12 06:53:36

标签: vb.net hex binarywriter

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

    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)。

有人介意帮忙吗? :)

1 个答案:

答案 0 :(得分: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))