EnHex / DeHex
EnHex converts a string to hexidecimal characters, which I designed for use with encryption routines that sometimes output unprintable characters. It's a simple way to convert unprintable characters into something printable. DeHex simply reverses the process.
Original Author: Jamie Richard Wilson
Inputs
EnHex Input: normal text
DeHex Input: text that has been "en-hexed"
Assumptions
The assumption is that any text sent to DeHex is in fact hexidecimal. I pulled this from my own personal coding toolbox so I haven't built in any error checking because it was written for use in a very controlled environment -- such as apps I've written that use encryption.
Returns
EnHex Return: text converted into hexidecimal characters
DeHex Return: the original text that was converted to hexidecimal characters using EnHex
Side Effects
Converting a string into hexidecimal format will effectively double the size of the string (hexidecial requires two characters for every "en-hexed character), so be sure to weigh the benefits of having printable text against the size of the result. I tend to only use this if I need to send encrypted data blocks through email or if I want a user to manually enter small amounts of encrypted data, such as a one-line registration number.
Code
Public Function EnHex(Data As String) As String
Dim iCount As Double
Dim sTemp As String
For iCount = 1 To Len(Data)
sTemp = Hex$(Asc(Mid$(Data, iCount, 1)))
If Len(sTemp) < 2 Then sTemp = "0" & sTemp
EnHex = EnHex & sTemp
Next iCount
End Function
Public Function DeHex(Data As String) As String
Dim iCount As Double
For iCount = 1 To Len(Data) Step 2
DeHex = DeHex & Chr$(Val("&H" & Mid$(Data, iCount, 2)))
Next iCount
End Function
Loading Comments ...
Comments
No comments have been added for this post.
You must be logged in to make a comment.