Jump to content

Base 36 converter


Nim199

Recommended Posts

Hi all,With VB.NET (and C#, i think) you cannot 'use' a variable by pluging in its 'name' to something. Hope this makes sence.Anyway, I created a Nase 36 converter system that should alow you (with VB.NET) to do the above.Place this function into your code, and also the 'vars' array:

    Dim vars(1000)       Function getvariablename(ByVal name As String)        Dim i As Integer = 0        Dim var        Dim tv As String = ""        Dim tn As Integer = 0        Dim tvars(name.Length) As Integer        Dim nvar As Long = 0        For i = 1 To name.Length            var = Mid(name, i, 1)            Select Case var                Case "a" : tn = 11                Case "b" : tn = 12                Case "c" : tn = 13                Case "d" : tn = 14                Case "e" : tn = 15                Case "f" : tn = 16                Case "g" : tn = 17                Case "h" : tn = 18                Case "i" : tn = 19                Case "j" : tn = 20                Case "k" : tn = 21                Case "l" : tn = 22                Case "m" : tn = 23                Case "n" : tn = 24                Case "o" : tn = 25                Case "p" : tn = 26                Case "q" : tn = 27                Case "r" : tn = 28                Case "s" : tn = 29                Case "t" : tn = 30                Case "u" : tn = 31                Case "v" : tn = 32                Case "w" : tn = 33                Case "x" : tn = 34                Case "y" : tn = 35                Case "z" : tn = 36            End Select            If tn = 0 Then                tn = CInt(var)            End If            tn = tn * 36 ^ (i - 1)            tvars(i) = tn            tn = 0        Next        nvar = 0        For i = 1 To name.Length            nvar = nvar + tvars(i)        Next        getvariablename = nvar    End Function

The function alows you to send of the 'name' of the variable in base 36 (0-9 & a-z) and it returns the deinary value, wich you then plug into the 'vars' array.In effect, this means you can have almost any variable name in the array. TRY IT!I have tried it out, and it works fine for me...Example of usage:

		vars(getvariablename("cool")) = "Hi"		MsgBox(vars(getvariablename("cool")))		vars(getvariablename("oolc")) = "ME!"		MsgBox(vars(getvariablename("oolc")))

This returns a message box with the text "Hi", then another saying "ME!"If the code doesn't work, or can be improved, please leave a post here.

Link to comment
Share on other sites

Also, if you want underscores!...Here is the function:

    Function getvariablename(ByVal name As String)        Dim i As Integer = 0        Dim var        Dim tv As String = ""        Dim tn As Integer = 0        Dim tvars(name.Length) As Integer        Dim nvar As Long = 0        For i = 1 To name.Length            var = Mid(name, i, 1)            Select Case var                Case "a" : tn = 11                Case "b" : tn = 12                Case "c" : tn = 13                Case "d" : tn = 14                Case "e" : tn = 15                Case "f" : tn = 16                Case "g" : tn = 17                Case "h" : tn = 18                Case "i" : tn = 19                Case "j" : tn = 20                Case "k" : tn = 21                Case "l" : tn = 22                Case "m" : tn = 23                Case "n" : tn = 24                Case "o" : tn = 25                Case "p" : tn = 26                Case "q" : tn = 27                Case "r" : tn = 28                Case "s" : tn = 29                Case "t" : tn = 30                Case "u" : tn = 31                Case "v" : tn = 32                Case "w" : tn = 33                Case "x" : tn = 34                Case "y" : tn = 35                Case "z" : tn = 36                Case "_" : tn = 37                ' simply add to the select case, and change al 36 to 37 etc to add new punctuation.            End Select            If tn = 0 Then                tn = CInt(var)            End If            tn = tn * 37 ^ (i - 1)            tvars(i) = tn            tn = 0        Next        nvar = 0        For i = 1 To name.Length            nvar = nvar + tvars(i)        Next        getvariablename = nvar    End Function

Also, if you need to increae the number of variables, just increase the size of 'vars' (the array).

Link to comment
Share on other sites

I will paste all the new versions in this post...The lower down they are, the better!Version 2.5 {Specifications: Range: 1-9, a-z & _ Notes: lowercase variables only (HI = hi); If ERRORS ocour, then recuce the number of 9s in the 'vars' array Max Variable Name Length: Current Estimate: 4 charactors Created: 02/12/07

Dim vars(9999999)    Function gvn(ByVal name As String)        Dim i As Integer = 0        Dim var        Dim tv As String = ""        Dim tn As Integer = 0        Dim tvars(name.Length) As Integer        Dim nvar As Long = 0        For i = 1 To name.Length            var = LCase(Mid(name, i, 1))            Select Case var                Case "a" : tn = 11                Case "b" : tn = 12                Case "c" : tn = 13                Case "d" : tn = 14                Case "e" : tn = 15                Case "f" : tn = 16                Case "g" : tn = 17                Case "h" : tn = 18                Case "i" : tn = 19                Case "j" : tn = 20                Case "k" : tn = 21                Case "l" : tn = 22                Case "m" : tn = 23                Case "n" : tn = 24                Case "o" : tn = 25                Case "p" : tn = 26                Case "q" : tn = 27                Case "r" : tn = 28                Case "s" : tn = 29                Case "t" : tn = 30                Case "u" : tn = 31                Case "v" : tn = 32                Case "w" : tn = 33                Case "x" : tn = 34                Case "y" : tn = 35                Case "z" : tn = 36                Case "_" : tn = 37            End Select            If tn = 0 Then                tn = CInt(var)            End If	    If tn <> 0 Then            	tn = tn * 37 ^ (i - 1)	    End If            tvars(i) = tn            tn = 0        Next        nvar = 0        For i = 1 To name.Length            nvar = nvar + tvars(i)        Next        gvn = nvar    End Function

}Version 2.7 {Specifications: Range: 1-9, a-z & _ Notes: lowercase variables only (HI = hi); If ERRORS ocour, then recuce the number of 9s in the 'vars' array; reduction in average variable name length in deninary due to the select case now based on most common letters; NEWEST VERSION Max Variable Name Length: Current Estimate: 4 charactors Created: 08/12/07

Dim vars(9999999)    Function gvn(ByVal name As String)        Dim i As Integer = 0        Dim var        Dim tv As String = ""        Dim tn As Integer = 0        Dim tvars(name.Length) As Integer        Dim nvar As Long = 0        For i = 1 To name.Length            var = LCase(Mid(name, i, 1))            Select Case var                Case "e" : tn = 11                Case "t" : tn = 12                Case "a" : tn = 13                Case "i" : tn = 14                Case "n" : tn = 15                Case "o" : tn = 16                Case "s" : tn = 17                Case "h" : tn = 18                Case "r" : tn = 19                Case "d" : tn = 20                Case "l" : tn = 21                Case "u" : tn = 22                Case "c" : tn = 23                Case "m" : tn = 24                Case "f" : tn = 25                Case "w" : tn = 26                Case "y" : tn = 27                Case "g" : tn = 28                Case "p" : tn = 29                Case "b" : tn = 30                Case "v" : tn = 31                Case "k" : tn = 32                Case "q" : tn = 33                Case "j" : tn = 34                Case "x" : tn = 35                Case "z" : tn = 36                Case "_" : tn = 37            End Select            If tn = 0 Then                tn = CInt(var)            End If	    If tn <> 0 Then            	tn = tn * 37 ^ (i - 1)	    End If            tvars(i) = tn            tn = 0        Next        nvar = 0        For i = 1 To name.Length            nvar = nvar + tvars(i)        Next        gvn = nvar    End Function

}Please post your experiences of this here (assuming it doesn't function properly) so that everyone can use them to their advantage. PLease do not PM me about his topic.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...