Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Public Function ReadHexStringToByte(ByVal Input As String) As Byte()
- Input = Input.ToUpper().Trim()
- If Input.Length = 0 Then GoTo fail
- Dim ValidChar As String = "0123456789ABCDEF"
- For Each character As Char In Input
- If Not ValidChar.Contains(character) Then GoTo fail
- Next
- If Input.Length Mod 2 = 1 Then Input = "0" & Input
- Dim ResultBytes As Byte() = New Byte(Input.Length / 2 - 1) {}
- Dim index As Integer = 0
- For i As Integer = 0 To Input.Length - 1 Step 2
- ResultBytes(index) = Convert.ToByte(Input.Substring(i, 2), 16)
- index += 1
- Next
- Return ResultBytes
- fail:
- Throw New Exception("Invalid HexString")
- End Function
Advertisement
Add Comment
Please, Sign In to add comment