2015-01-17 58 views
-1

這是我的代碼。減去文本框(十六進制)

它讀取* .bin文件字節,並在TextBox1中顯示它

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 

現在我想在TextBox2中添加減法。

例如:

H2E - BC 
H2F - CD 
BCCD - 2222 = 9AABB 
Textbox2. = result 

我這個嘗試,但它給出的結果在十進制

TextBox2.Text = Val(TextBox1.Text) - Val("2222") 
+0

這不是JS,這是VB .NET或Visual Basic!相應地重新標記。 –

+2

而且你正在嘗試對兩個字符串類型**進行**減法操作,或許你想要做的是對兩個十六進制數字執行**減法操作。** – ilgaar

回答

1

嘗試這樣的事情了:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 
    Dim hex As String = String.Join("", TextBox1.Text.Trim.Split(" ")) ' Assuming "BC CD" is in the box 
    If hex.Length = 4 Then 
     Try 
      Dim intA As Integer = Convert.ToInt32(hex, 16) 
      Dim intB As Integer = Convert.ToInt32("2222", 16) 
      Dim intC As Integer = intA - intB 
      TextBox2.Text = intC.ToString("X2") 
     Catch ex As Exception 
      MessageBox.Show("Invalid Hexadecimal Value") 
     End Try 
    Else 
     MessageBox.Show("Invalid Hexadecimal Value") 
    End If 
End Sub