need help

i build a program in vb express that can read a binary file in hex mode by inserting the hex location i need help to seek and hex and write after the hex i seeked or searched i need the code in vb express how can i build a search for hex in file and write hex using the fileput after the hex i searched


Answer this question

need help

  • Rashar

    Let's go back to basics again.

    If you want to search for '"00B597F1"

    Where did that come from (I'm asking the source to determine it's radix). Is it user input Or is it already binary

    Your problem has three steps.

    1.) Radix conversion (if needed - depending upon the source)

    2.) Searching

    3.) insertion

    So let's think about the first

    You are reading BINARY... it's all binary.

    Your are then reading string represention of that binary an converting it into hex strings...

    That's why it's important to know what the source of the matches are.

    Also what are the data boundaries Are you expecting a match to begin anywhere in a stream Or is it on long word boundaries



  • KitZ_CK

    ReneeC, 

    I believe you are missing an important aspect of the question.

    The final product is that this file has a checksum or CRC appended to the file being altered.  Once the new data is added in the middle of the file, a new checksum or CRC needs to be calculated and replace the existing one so the application will still view the data file as being valid.

    I wish I could help, I came across this thread looking for a VB function to calculate CRC16s and CRC32s of data.  I believe the origional poster is ultimately looking for the same.

     

    Rob


  • Sara12534

    thanks for the help see this " all the program code "

    Option Strict Off
    Option Explicit On
    Imports VB = Microsoft.VisualBasic
    Friend Class Form1
     Inherits System.Windows.Forms.Form
        Private Sub Command1_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Command1.Click
            Dim FileName As String
            Dim Hardware1 As Byte
            Dim Hardware2 As Byte
            Dim Software1 As Byte
            Dim Software2 As Byte
            Dim Model1 As Byte
            Dim Model2 As Byte
            FileName = Text1.Text

            FileOpen(1, FileName, OpenMode.Binary)
            FileGet(1, Hardware1, 19)
            FileGet(1, Hardware2, 20)
            Label2.Text = "Hardware: " & Hex(Hardware1) & Hex(Hardware2)
            FileGet(1, Software1, 23)
            FileGet(1, Software2, 24)
            Label3.Text = "Software: " & Hex(Software1) & Hex(Software2)
            FileGet(1, Model1, 27)
            FileGet(1, Model2, 28)
            Label4.Text = "Modle ID: " & Hex(Model1) & Hex(Model2)
            FileClose(1)

        End Sub
     
     Private Sub Command2_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Command2.Click
      

      CommonDialog1Open.Filter = "Bin Files|*.bin"
      CommonDialog1Open.ShowDialog()
      
      Text1.Text = CommonDialog1Open.FileName
      
      
     End Sub
     
     Private Sub Command3_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Command3.Click
      Dim Soft, Model As String
      Dim Soft1, Model1 As String
      Dim Soft2, Model2 As String
      Dim FileName As String
      Dim Software1, Software2 As Byte
      Dim Model_id1, Model_id2 As Byte
      FileName = Text1.Text
      Soft = Text2.Text
      Model = Text3.Text
      Soft1 = "&h" & VB.Left(Soft, 2)
      Soft2 = "&h" & VB.Right(Soft, 2)
      Model1 = "&h" & VB.Left(Model, 2)
      Model2 = "&h" & VB.Right(Model, 2) 
      Software1 = Val(Soft1)
      Software2 = Val(Soft2)
      Model_id1 = Val(Model1)
      Model_id2 = Val(Model2)
      FileOpen(1, FileName, OpenMode.Binary)
      FilePut(1, Software1, 23)
      FilePut(1, Software2, 24)
      FilePut(1, Model_id1, 27)
      FilePut(1, Model_id2, 28)
      FileClose(1) 
     End Sub

    the file it is alrady binary and choosed by the user

    now if u see in this code that i can view the hex at the command1_click (here i knew the place of the hex in the file)

    and i want to do search in the file of an string in hex like "00B597F1" but the problem is that if i change another binary file the place of this code will change so i need to know the place of this string "00B597F1" and when i knew the place i want to insert another string after this string , example :

    after the search if this code 00B597F1 starts in hex at 40 so the end of this string will be 47

    and then i want to insert a new string after the 47 and when i finished the new insert i want to do " CheckSum " and "crc-16" ( the checksum and the crc-16 i do with a hex editor to let the machean accept the file ) and i know how to select the range in the file that i want to do checksum and crc-16 but i don't know how to do checksum and crc-16 in the visualbasic

    thanks again for the help



  • BDaul

    Let's see if this helps.......

    The object of the game is to match the radix's (sp )  so that the content is the same when you do your search.

    You may wat to convert you hex representation back into a binary number and serach the original binary string where both are compared in binary.

    Then you have your real offset into the buffer. If you know that then you can create a new buffer

    1.) Put all contents, including what you've found (its length)  into the new buffer. 2.) Append the contents you want to append
    3.) Append the remaining contents.
    4.) Close the old file
    5.) Rename the old file to some random file name
    4.) Write the new buffer with the name of the old file
    5.) Once you know the new file is written - you may then safely delete the renamed file.

    Does this help It will work.. I promise.

    It will probably be best to do this with a byte stream.

    You can practice this by reading in the file in bytes

    Put the comparision data into unsigned bytes - not in hex but in binary

    So if you are looking for Hex AA, BB, CC, DD

    hex AA or 170. goes into unByte 0   BB or 187. goes into unbyte(1) etc

    So you have to know how many bytes to allocate for the comparision by the magnitude of the number.

    Search the input buffer in bytes

    dim I as integer = 0
    dim ii as integer = 0

    dim bLeave as boolean
    Dim success as boolean success = false

    for i  = Buffer.length-1


       if buffer(i) = unbyte(0) then  '.... compare the next three bytes and if they are equal the offset is i+3 ***

       if i + NumberOfBytesToCompare => Buffer.length-1  then exit for



       for ii as integer = 0 to NumberOfBytesToCompare
          if buffer(i + ii) <> unbyte(ii +1) then
             leave = true
            exit for
       next
     if ii == numberOfBytesToCompare=1 then
            success = true
            Exit loop
     end if
      if leave exit loop
    next

    if success is true you have a match then you can:


    *** Do this also in a do loop where the count is the number of bytes you are comparing -1 (This is based zero). If there is a total match exit the loop


    write the i+3 bytes into a new buffer
    Add what you want to append to the new buffer
    write byes I+4 to buffer.length to the new buffer

    You now have your new buffer and can follow the steps above.


    I hope this helps.

    Renee





  • OliverKofoed

    thanks for the help

    see this code and u will understand what i meen

    Private Sub Command1_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Command1.Click
            Dim FileName As String
            Dim case1 As Byte
            Dim case2 As Byte
            Dim case3 As Byte
            Dim case4 As Byte
            Dim case5 As Byte
            Dim case6 As Byte
           
            FileName = Text1.Text

            FileOpen(1, FileName, OpenMode.Binary)
            FileOpen(1, FileName, OpenMode.Binary)
            FileGet(1, case1, 19)
            FileGet(1, case2, 20)
            Label2.Text = "master1: " & Hex(case1) & Hex(case2)
            FileGet(1,case3, 23)
            FileGet(1,case4, 24)
            Label3.Text = "master2: " & Hex(case3) & Hex(case4)
            FileGet(1, case5, 27)
            FileGet(1, case6, 28)
            Label4.Text = "master3: " & Hex(case5) & Hex(case5)
           FileClose(1)
    End Sub
     in this code i get the hex and display it, what i need is that i want to search hex in the file

    like "00B597F1" and when i find it i want to insert an hex after that number and one more thing if i select hex in the file can i do "checksum or crc-16 the hex i select and how"

    and thanks for the help



  • maestro49m

    Farah,

    Could we drop back to basics for a moment

    First of all in a very real way, the content of all files everywhere are in binary and I think that's important to remember. It may be organized to decsribe something that we'll call ascii or organized into something that we call hex. But it's all binary.

    Am I hearing you say that you want to read a file, and format the data in hex, and then do a search on hex patterns

    In that case, in memory format the stream into hex and search the formatted stream for what you are looking. Do so in a way that allows you to refer back to the orginal offsets of the data.

    You may want to look at the instr function. :)

  • need help