1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
| Imports System.IO
Imports Microsoft.VisualBasic.CompilerServices
Public Class Form1
Dim parcourir As String = My.Computer.FileSystem.SpecialDirectories.Desktop & "\Hexadécimal.bin"
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
ComboBox1.SelectedIndex = 4 'afficher le numero dans le combobox1.text à l'ouverture du form1
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Try
If TextBox1.Text.Length > 0 Then
Effacer() 'effacer tous il y a dans label2.text et textbox2.text
Dim fs As New FileStream(parcourir, FileMode.Open, FileAccess.Read, FileShare.Read)
Dim reader As New BinaryReader(fs)
Dim Search As String = HexSearch(TextBox1.Text, fs) 'recherche la position du hexadécimal
If Search Then ' il affichera si il trouve ou pas dans le fichier
Label2.Text = Conversion.Hex(Search) 'affiche la position du hexadecimal dans label2.text
reader.BaseStream.Position = "&H" & Label2.Text 'connaitre la position avant afficher dans textbox2.text
TextBox2.Text = BitConverter.ToString(reader.ReadBytes(ComboBox1.Text)).Replace("-", " ") 'afficher la taille hexadécimal par rapport combobox1.txt ou remplacer reader.ReadBytes(une valeur de votre choix ex : 4,8,12,16...etc)).Replace("-", " ")
Else
MessageBox.Show("Non Trouver le Hexadécimal!", "Erreur", MessageBoxButtons.OK, MessageBoxIcon.Error) 'message d'erreur il trouve pas le hexadécimal dans votre fichier
End If
fs.Close()
reader.Close()
Else
MessageBox.Show("Veuillez entrer un Hexadécimal pour rechercher", "Erreur", MessageBoxButtons.OK, MessageBoxIcon.Error) 'méme chose pour textbox1.text
End If
Catch exception1 As Exception
ProjectData.SetProjectError(exception1)
Dim exception As Exception = exception1
Interaction.MsgBox("Erreur", MsgBoxStyle.Critical, "Erreur Lecture") 'si il y a eu un probléme de lecture du fichier
ProjectData.ClearProjectError()
End Try
End Sub
Private Function HexSearch(ByVal str As String, ByVal fs As FileStream) As Long
Dim position As Integer = 0
Dim reader As New BinaryReader(fs)
For i As Long = 0 To reader.BaseStream.Length - 1
fs.Seek(i, SeekOrigin.Begin)
If BitConverter.ToString(reader.ReadBytes(str.Length / 2)).Replace("-", "") = str Then
position = i
Exit For
End If
Next i
Return position
End Function
Private Sub TextBox1_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox1.TextChanged
TextBox1.Text = TextBox1.Text.Replace(" ", "") 'supprimer les espaces en hexadécimal
End Sub
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
If TextBox2.Text.Length > 0 Then
Dim fs As New FileStream(parcourir, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite)
Dim writer As New BinaryWriter(fs)
writer.BaseStream.Position = "&H" & Label2.Text
Dim hex = Split(TextBox2.Text, " ")
Dim data(UBound(hex)) As Byte
For i = LBound(hex) To UBound(hex)
data(i) = Val("&H" & hex(i))
Next i
writer.Write(data)
fs.Close()
writer.Close()
MessageBox.Show("Sauvegarder Reussite !", "Sauvegarder", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End Sub
Private Sub Effacer()
Label2.Text = Nothing
TextBox2.Clear()
End Sub
End Class |
Partager