Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- I am using this as a seperate class: Public Class HexConverter
- Private Property _HexString As String
- Private Property _HexBytes As Byte()
- Public Property HexString As String
- Get
- Return _HexString
- End Get
- Set(ByVal value As String)
- _HexString = value
- _HexBytes = HexToBytes(value)
- End Set
- End Property
- Public Property HexBytes As Byte()
- Get
- Return _HexBytes
- End Get
- Set(ByVal value As Byte())
- _HexBytes = HexBytes
- _HexString = BytesToHexString(value)
- End Set
- End Property
- Private Function HexToBytes(ByVal Hex As String) As Byte()
- Dim Result As New List(Of Byte)
- If Not HexIsValid(FormatHex(Hex)) Then Throw New Exception("Invalid hexidecimal number cannot be converted!")
- For I = 0 To FormatHex(Hex).Length - 1 Step 2
- Result.Add(Convert.ToByte(FormatHex(Hex).Substring(I, 2), 16))
- Next : Return Result.ToArray()
- End Function
- Private Function BytesToHexString(ByVal Bytes() As Byte) As String
- Return "&H" & Replace(BitConverter.ToString(Bytes, 0), "-", "&H")
- End Function
- Private Function HexIsValid(ByVal Hex As String) As Boolean
- For Each Nibble As String In Hex
- If "0123456789ABCDEFabcdef".IndexOf(Nibble, 0) = -1 Then Return False
- Next : Return True
- End Function
- Private Function FormatHex(ByVal Hex As String) As String
- Hex = Replace(Hex, "&h", "") : Hex = Replace(Hex, "&H", "")
- If (Len(Hex) Mod 2) = 1 Then Hex = "0" & Hex
- Return Hex
- End Function
- Public Shadows Function ToString() As String
- Return _HexString
- End Function
- End Class
- This is how Im calling it (Button Click)
- Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
- 'Your Hex String
- Dim SampleHexString As String = newkey.Text
- 'Create a new Hex Converter
- Dim HexConverter1 As New HexConverter
- 'Change the value of the Hexconverter by setting the string
- HexConverter1.HexString = SampleHexString
- 'Get the conversion result from the hexconverter
- Dim ResultBytes As Byte() = HexConverter1.HexBytes
- 'Create a new hexconverter to test that it works
- Dim HexConverter2 As New HexConverter
- 'This time change the value of the hex converter by setting its bytes instead of
- 'setting its string
- HexConverter2.HexBytes = HexConverter1.HexBytes
- 'check if the conversion matches the original string
- If SampleHexString = HexConverter2.ToString And SampleHexString = HexConverter2.HexString Then
- MsgBox("The class works.")
- Else
- MsgBox("The class does not work")
- End If
- End Sub
- When I call it I am trying to take the string from the textbox as seen in this line: Dim SampleHexString As String = newkey.Text
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement