Results 1 to 4 of 4

Thread: Substract from textbox (hexadecimal)

  1. #1

    Thread Starter
    Member
    Join Date
    Jan 2015
    Posts
    46

    Substract from textbox (hexadecimal)

    Hello. Here is my code.
    It reading bytes from *.bin file and show it in textbox1

    Code:
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
           Dim OFD As New OpenFileDialog
            Dim fullFile() As Byte
     
            If OFD.ShowDialog = Windows.Forms.DialogResult.OK Then
                fullFile = File.ReadAllBytes(OFD.FileName)
                TextBox1.AppendText(fullFile(&H2E).ToString("X2") & " ")
                TextBox1.AppendText(fullFile(&H2F).ToString("X2"))
            End If
     
        End Sub
    Now I want to add substraction in textbox2.
    example:
    H2E - BC
    H2F - CD

    BCCD - 2222 = 9AABB

    Textbox2. = result

    I tired with this, but it gives result in decimal

    Code:
       TextBox2.Text = Val(TextBox1.Text) - Val("2222")
    Last edited by ymurok; Jan 17th, 2015 at 09:02 AM.

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    35,237

    Re: Substract from textbox (hexadecimal)

    I'd be surprised if that code even came close to working. Val will start parsing the text until it finds a character that it doesn't recognize, then it will return a double of whatever it has parsed thus far. So, Val("9AABB") should result in 9, and Val("BCCD") should result in 0. I don't believe that Val cares at all about what the string contains, it just works character by character.

    You would be far better off holding the values in variables rather than textboxes. If you hold the values in textboxes, then the values HAVE to be strings, which means that you will be converting to and from strings for any math, which is somewhat slower. Of course, if the user will be entering the "2222", then it wouldn't matter, as they will be entering a string anyways. It isn't clear whether the user will be entering the value for subtracting, or not, though. Still, as much as possible, keep the values in integers or bytes, not in strings. That will make the math VASTLY simpler, since you can just subtract.

    As it is, you have to take the value from Textbox2.Text, convert it to an integer, subtract the value, convert the result back to a string, and put it back in TextBox2.Text. There is no shortcut to this.
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    Member
    Join Date
    Jan 2015
    Posts
    46

    Re: Substract from textbox (hexadecimal)

    Quote Originally Posted by Shaggy Hiker View Post
    I'd be surprised if that code even came close to working. Val will start parsing the text until it finds a character that it doesn't recognize, then it will return a double of whatever it has parsed thus far. So, Val("9AABB") should result in 9, and Val("BCCD") should result in 0. I don't believe that Val cares at all about what the string contains, it just works character by character.

    You would be far better off holding the values in variables rather than textboxes. If you hold the values in textboxes, then the values HAVE to be strings, which means that you will be converting to and from strings for any math, which is somewhat slower. Of course, if the user will be entering the "2222", then it wouldn't matter, as they will be entering a string anyways. It isn't clear whether the user will be entering the value for subtracting, or not, though. Still, as much as possible, keep the values in integers or bytes, not in strings. That will make the math VASTLY simpler, since you can just subtract.

    As it is, you have to take the value from Textbox2.Text, convert it to an integer, subtract the value, convert the result back to a string, and put it back in TextBox2.Text. There is no shortcut to this.
    So, look at this code

    Code:
       If OFD.ShowDialog = Windows.Forms.DialogResult.OK Then
         Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim OFD As New OpenFileDialog
            If OFD.ShowDialog = Windows.Forms.DialogResult.OK Then
                Using fs As New FileStream(OFD.FileName, FileMode.Open)
                    fs.Position = 30
                    Using br As New BinaryReader(fs)
                        Dim value As UInt16 = br.ReadUInt16()
                        TextBox1.Text = value.ToString("X4")
                    End Using
                End Using
            End If
    
        End Sub
        End If
    It is almost good, but I don't know how to change it from little endian to big endian. It gives incorrect values.
    In file is: 95 AA
    Program reading is as: AA 95
    Just need to fix it, and my program is done.
    Only you can help me here.

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    35,237

    Re: Substract from textbox (hexadecimal)

    Is it always just two bytes? If so, you could just move the bytes around. If it is more than two bytes, then moving them around would be kind of a pain. For two bytes you could do:

    Dim newValue as UInt16 = (((value >> 8) And &HF) Or ((value << 8) And &HF0))

    That may be more than needs to be done, but VB tends to propagate the high bit when bit shifting, so the masking may be needed. What I am doing is taking the original two byte value, shifting the high bits to the low, clearing the high bits, and also taking the value, shifting the low bits to the high and masking out the low, then oring the two together. There is probably a simpler way.
    My usual boring signature: Nothing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width