Well I am building an ID number generator. How can I loop through a string and find the ascii value of each character. Example
String Mxjerrett
Converted 77120106101114114101116116
Printable View
Well I am building an ID number generator. How can I loop through a string and find the ascii value of each character. Example
String Mxjerrett
Converted 77120106101114114101116116
Code:Dim i As Integer
Dim strID As String
Dim strName As String
strName = "Mxjerrett"
strID = ""
For i = 1 to Len(strName)
strID = strID & Asc(Mid(strName, i, 1))
Next
I think he wants to convert "77120106101114114101116116" back to "Mxjerrett"
That code looks like it will do what he already did. But, not sure how you would convert that back without having some sort of seperator between each number that is a letter.
Looking at the end of the converted string, how would you say "Convert 16, then 116" or "Convert 61, then 16"
You could have it insert something between each converted letter, so it would be like.. "77.120.106...." but that would be easy to reverse (though not like what you have now will stump anyone ;))
Well he said he's generating an ID number. He said he wanted to loop through a string and find the ascii value for each char.
What I would have done with your suggested problem is this:
The ID would be "27731203106310131143114310131163116"Code:Dim i As Integer
Dim strID As String
Dim strName As String
Dim strAscii As String
strName = "Mxjerrett"
strID = ""
For i = 1 to Len(strName)
strAscii = Asc(Mid(strName, i, 1))
strID = strID & Len(strAscii) & strAscii
Next
This will be easy to convert back to a name, but because the 3's look like delimiters it's still not perfect. From there I'd chop it into groups of 2 and convert it to 2 char hex (6 = 06, F = 0F) leaving "1B490C030A3F0A0D0B2B0B2B0A0D0B3F0B06". Simple, I know. And nowhere near foolproof. But easy to convert back to a name, and not obvious.
Here's some quick code:
Code:Dim i As Integer
Dim strID As String
Dim strTmp As String
Dim strHex As String
Dim strName As String
Dim strAscii As String
strName = "Mxjerrett"
strID = ""
For i = 1 To Len(strName)
strAscii = Asc(Mid(strName, i, 1))
strID = strID & Len(strAscii) & strAscii
Next
'strID = 27731203106310131143114310131163116
strTmp = ""
For i = 1 To Len(strID) Step 2
strHex = Right("00" & Hex(Val(Mid(strID, i, 2))), 2)
strTmp = strTmp & strHex
Next
'strTmp = 1B490C030A3F0A0D0B2B0B2B0A0D0B3F0B06
Your code works perfectly, but now I need help deconverting it. I have the program putting - in between each character, but now I need to try to convert it back to characters. Ex:
My ID: 77-88-74-69-82-82-69-84-84
My Username (yes I know it is caps): MXJERRETT
Nevermind guys, I wrote my own code that seems to work 100% perfectly. Here is the code in case you guys wanted to know:
Code:Dim strAscii, strID, strName As String
strID = Replace(txtID, "-", "")
For i = 1 To Len(strID) / 2
strAscii = Chr(Right(Left(strID, i * 2), 2))
strName = strName & strAscii
Next
txtName = strName