Answered Writing a Varible to the hex

  • Friday, April 13, 2012 11:07 PM
     
     

    hi i have made a program that converts a number you put into a text field into hex

    What i now want to is to be able to write that variable into the file as hex still

    eg 
    bw.Write(&H(Variable))

    if that worked that is what i want to do but it doesn't can you help me

    Thanks for any help

All Replies

  • Friday, April 13, 2012 11:18 PM
     
     
    bw.Write("&H" & Variable)

    thanks for any help

  • Friday, April 13, 2012 11:25 PM
     
      Has Code

    Thanks for the quick response but it didn't work this still make that appear as text im testing it on a text file in Hex workshop BTW

    here is my code in case i explained it bad

    Private Sub CmdModAtsumo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdModAtsumo.Click
            Dim intNum1 As Integer
            Dim hex_val As String
    
            Dim fs As System.IO.FileStream
            Dim bw As System.IO.BinaryWriter
            fs = New System.IO.FileStream(OFD1.FileName, IO.FileMode.OpenOrCreate) 'Uses the file you just opened
            bw = New System.IO.BinaryWriter(fs) 'tells the binary writer to write to the file you opened
    
            intNum1 = Integer.Parse(TextBox1.Text)
            hex_val = Hex(intNum1)
    
    
            fs.Position = &H2C871 'offset to write 
            bw.Write("&H" & hex_val) 
    
            fs.Close()
            bw.Close()
    
    
    
    
    
            MsgBox(hex_val)
    
            MsgBox("Modded Atsumo")
    
        End Sub
    also the comments are there so i can check back if i forget how i did that certain thing
  • Saturday, April 14, 2012 12:47 AM
     
     

    It's not clear what you want to write. The number in the textbox is a String in decimal format. You want to convert it to a number in hex format which is also a String and write it to the file. Is this right? If so, you will probably want to format the number with leading zeros. You must also chose the right character encoding when writing the String to the file. If you don't specify it when creating the BinaryWriter, UTF-8 is used. Is the prefix "&H" appropriate? It's usually only used in VB source code.

    I suspect you just want to write the value itself to the file, not it's String representation. In that case you just have to write

        bw.Write(Variable)

    If variable is of type integer, it writes  4 bytes.


    Armin

  • Saturday, April 14, 2012 6:47 AM
     
     

    Something like this?

        Sub Main()
          Dim someVar As Integer = 123
          Console.WriteLine("someVar = " & someVar.ToString("X"))
          Console.ReadLine()
        End Sub


    Regards David R
    ---------------------------------------------------------------
    The great thing about Object Oriented code is that it can make small, simple problems look like large, complex ones.
    Object-oriented programming offers a sustainable way to write spaghetti code. - Paul Graham.
    Every program eventually becomes rococo, and then rubble. - Alan Perlis
    The only valid measurement of code quality: WTFs/minute.

  • Saturday, April 14, 2012 7:12 AM
     
     

    Yes you can do it, it is very easy. But normally like Armin wrote in fact as well it makes no sense.

    However be aware that VB has an Hex function which returns a string in a Hex representation of a number

    http://msdn.microsoft.com/en-us/library/963zt96e(v=VS.80).aspx


    Success
    Cor

  • Saturday, April 14, 2012 8:10 AM
     
     

    Provide an an example of the number that the user could enter into the text box, and how that portion of the file will be displayed in HEX Workshop before and after you make the change.

  • Saturday, April 14, 2012 1:02 PM
     
     

    This is what the application looks like and as you can see it converts the number 8999 into hex which is 2327

    it then writes to the file using this code

     bw.Write(hex_val)

    hex_val contains the text string 2327

    this is what it does to the file im using a .txt just as a test


    Sorry for the size im not sure how to shrink it but as you can see it write the hex_val to the text half not the hex now this is what i want it to do


  • Saturday, April 14, 2012 1:04 PM
     
     

    sorry for the second message you can only put two images per message

    i thank everyone that has replied as you are trying to help



    BTW im not that good with VB so if you reply can you try to make it idiot friendly

  • Saturday, April 14, 2012 1:24 PM
     
      Has Code

    The file does not contain any hex values.  The value you are trying to write is an integer consisting of four bytes.  You can probably just write the integer value into the file at that point, but that assumes that the file follows the Microsoft/Intel storage format for integers.  If unsure, you could extract each byte value and write all four of them into successive locations, one at a time.

    This code shows how to get the four byte values that you need to write into the four successive file locations - you will need to work out the order for yourself.  You will write each byte from the byte aray, NOT the hex strings, using the same sort of code that you already have.   The example below is only using hex as a convenient way to view the byte values.

            Dim I As Integer = Val(TextBox1.Text)
            Dim B As Byte() = BitConverter.GetBytes(I)
            Label1.Text = Hex(B(0)) & " " & Hex(B(1)) & " " & Hex(B(2)) & " " & Hex(B(3))

    I am assuming that you must write all four bytes, based o yout first image.  It is possible that the value is UInt16 instead of Integer, in which case you would write only two bytes.

  • Saturday, April 14, 2012 1:56 PM
     
      Has Code

    Thanks for the help but im afraid i still cant get it to write it in the Hex bit it's still writing as actual readable text

    This is my new code knowing me i have done something wrong

    Btw this program is ment to be a modding tool hence it needing to write in the hex part

    Private Sub CmdModAtsumo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdModAtsumo.Click
        
            Dim fs As System.IO.FileStream
            Dim bw As System.IO.BinaryWriter
            fs = New System.IO.FileStream(OFD1.FileName, IO.FileMode.OpenOrCreate) 'Uses the file you just opened
            bw = New System.IO.BinaryWriter(fs) 'tells the binary writer to write to the file you opened
    
            Dim I As Integer = Val(TextBox1.Text)
            Dim B As Byte() = BitConverter.GetBytes(I)
            Label5.Text = Hex(B(0)) & " " & Hex(B(1))
    
    
            fs.Position = &H2C871 'offset to write 
            bw.Write(Hex(B(0)))
    
            fs.Position = &H2C872 'offset to write 
            bw.Write(Hex(B(1)))
    
            fs.Close()
            bw.Close()


  • Saturday, April 14, 2012 2:14 PM
     
     Answered

    You are still trying to write hex - the file does not contain hex values.  You must write four bytes.

            fs.Position = &H2C871 'offset to write
            bw
    .Write(B(0))

            fs.Position = &H2C872 'offset to write
            bw
    .Write(B(1))

            fs.Position = &H2C873 'offset to write
            bw
    .Write(B(2))

            fs.Position = &H2C874 'offset to write
            bw
    .Write(B(3))

    • Marked As Answer by cheesecake415 Saturday, April 14, 2012 2:18 PM
    •  
  • Saturday, April 14, 2012 2:18 PM
     
     

    Thank you so much i understand now