Advertisement
Guest User

Untitled

a guest
Feb 19th, 2013
376
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.00 KB | None | 0 0
  1. I am using this as a seperate class: Public Class HexConverter
  2. Private Property _HexString As String
  3. Private Property _HexBytes As Byte()
  4. Public Property HexString As String
  5. Get
  6. Return _HexString
  7. End Get
  8. Set(ByVal value As String)
  9. _HexString = value
  10. _HexBytes = HexToBytes(value)
  11. End Set
  12. End Property
  13. Public Property HexBytes As Byte()
  14. Get
  15. Return _HexBytes
  16. End Get
  17. Set(ByVal value As Byte())
  18. _HexBytes = HexBytes
  19. _HexString = BytesToHexString(value)
  20. End Set
  21. End Property
  22. Private Function HexToBytes(ByVal Hex As String) As Byte()
  23. Dim Result As New List(Of Byte)
  24. If Not HexIsValid(FormatHex(Hex)) Then Throw New Exception("Invalid hexidecimal number cannot be converted!")
  25. For I = 0 To FormatHex(Hex).Length - 1 Step 2
  26. Result.Add(Convert.ToByte(FormatHex(Hex).Substring(I, 2), 16))
  27. Next : Return Result.ToArray()
  28. End Function
  29. Private Function BytesToHexString(ByVal Bytes() As Byte) As String
  30. Return "&H" & Replace(BitConverter.ToString(Bytes, 0), "-", "&H")
  31. End Function
  32. Private Function HexIsValid(ByVal Hex As String) As Boolean
  33. For Each Nibble As String In Hex
  34. If "0123456789ABCDEFabcdef".IndexOf(Nibble, 0) = -1 Then Return False
  35. Next : Return True
  36. End Function
  37. Private Function FormatHex(ByVal Hex As String) As String
  38. Hex = Replace(Hex, "&h", "") : Hex = Replace(Hex, "&H", "")
  39. If (Len(Hex) Mod 2) = 1 Then Hex = "0" & Hex
  40. Return Hex
  41. End Function
  42. Public Shadows Function ToString() As String
  43. Return _HexString
  44. End Function
  45. End Class
  46.  
  47. This is how Im calling it (Button Click)
  48. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  49. 'Your Hex String
  50. Dim SampleHexString As String = newkey.Text
  51. 'Create a new Hex Converter
  52. Dim HexConverter1 As New HexConverter
  53. 'Change the value of the Hexconverter by setting the string
  54. HexConverter1.HexString = SampleHexString
  55. 'Get the conversion result from the hexconverter
  56. Dim ResultBytes As Byte() = HexConverter1.HexBytes
  57. 'Create a new hexconverter to test that it works
  58. Dim HexConverter2 As New HexConverter
  59. 'This time change the value of the hex converter by setting its bytes instead of
  60. 'setting its string
  61. HexConverter2.HexBytes = HexConverter1.HexBytes
  62. 'check if the conversion matches the original string
  63. If SampleHexString = HexConverter2.ToString And SampleHexString = HexConverter2.HexString Then
  64. MsgBox("The class works.")
  65. Else
  66. MsgBox("The class does not work")
  67. End If
  68. End Sub
  69.  
  70. 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