Question

I've got a problem with my code. I put the offset value into listbox, then I read it and seek it in file. I use the following code.

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

the problem is: I need to use tablica(0) as an offset, tablica(1) and tablica(2) as bytes. What I want to do is open the file, select the offset and replace with tablica(2). czytana got a format "tablica(0): tablica(1) tablica(2).

Does somebody mind helping? :)

Was it helpful?

Solution

From your comments, I understand that you have the following inputs:

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

Your code should look like:

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

Where:

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))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top