Stumped on a conversion.

# Thread: Stumped on a conversion.

1. Robert Kozar Guest

## Stumped on a conversion.

Ok whats the vbscript way to convert a value from base32 to decimal?<BR><BR>I know of the javascript way but was looking for a way to use vbscript. any and all help would be nice.<BR><BR>-Thx

2. Senior Member
Join Date
Dec 1969
Posts
96,118

## RE: Stumped on a conversion.

From base 32??? What characters are you using to represent the values beyond 15? We use 0123456789ABCDEF for hex (base 16), but so far as I know there aren&#039t any standard characters beyond that point. <BR><BR>Hmmm...<BR><BR>&lt;HTML&#062;&lt;BODY&#062 ;<BR><BR>&lt;%<BR>&#039 these digits will handle bases from 2 to 16...just add to <BR>&#039 the list to go beyond that point!<BR>&#039<BR>CONST digits = "0123456789ABCDEF"<BR><BR>&#039 pass in number *as a string* and convert...<BR>Function convertBase( numAsStr, fromBase, toBase )<BR>&nbsp;&nbsp;&nbsp;&nbsp;&#039 first, we conver the string to an internal number<BR>&nbsp;&nbsp;&nbsp;&nbsp;Dim num, d, result<BR>&nbsp;&nbsp;&nbsp;&nbsp;num = 0<BR>&nbsp;&nbsp;&nbsp;&nbsp;For i = 1 To Len(numAsStr)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb sp;&nbsp;&nbsp;d = InStr( 1, digits, Mid(numAsStr,i,1), vbTextCompare )<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb sp;If d = 0 OR d &#062; fromBase Then <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;&nbsp;&nbsp;&nbsp;&nbsp;convertBase = "**ERROR** digit out of range: " & Mid(numAsStr,i,1)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp ;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Exit Function<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;End If<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;num = num * fromBase + ( d - 1 )<BR>&nbsp;&nbsp;&nbsp;&nbsp;Next<BR>&nbsp;&nbsp;& nbsp;&nbsp;&#039 now num is value in internal form...<BR>&nbsp;&nbsp;&nbsp;&nbsp;&#039 convert it back out...<BR>&nbsp;&nbsp;&nbsp;&nbsp;result = ""<BR>&nbsp;&nbsp;&nbsp;&nbsp;Do While Num &#062; 0<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb sp;d = Num MOD toBase<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;&nbsp;result = Mid(digits,d+1,1) & result<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;&nbsp;Num = Num toBase<BR>&nbsp;&nbsp;&nbsp;&nbsp;Loop<BR>&nbsp;&n bsp;&nbsp;&nbsp;convertBase = result<BR>End Function<BR>%&#062;<BR><BR>convertBase( "37512", 10, 16 ) is &lt;% = convertBase( "37512", 10, 16 ) %&#062;&lt;BR&#062;<BR>convertBase( "37512", 10, 8 ) is &lt;% = convertBase( "37512", 10, 8 ) %&#062;&lt;BR&#062;<BR>convertBase( "37512", 10, 3 ) is &lt;% = convertBase( "37512", 10, 3 ) %&#062;&lt;BR&#062;<BR>convertBase( "37512", 10, 2 ) is &lt;% = convertBase( "37512", 10, 2 ) %&#062;&lt;BR&#062;<BR>convertBase( "37512", 10, 10 ) is &lt;% = convertBase( "37512", 10, 10 ) %&#062;&lt;BR&#062;<BR>convertBase( "53", 10, 2 ) is &lt;% = convertBase( "53", 10, 2 ) %&#062;&lt;BR&#062;<BR>convertBase( "FfFe", 16, 10 ) is &lt;% = convertBase( "FfFe", 16, 10 ) %&#062;&lt;BR&#062;<BR>convertBase( "01100100", 2, 10 ) is &lt;% = convertBase( "01100100", 2, 10 ) %&#062;&lt;BR&#062;<BR><BR>&lt;/BODY&#062;&lt;/HTML&#062;<BR><BR>***********************<BR><BR>N OTE: If you want the digits going up to base 32 to be case sensitive, then you need to change the vbTextCompare in the call to INSTR. As it is, it allows mixed case (see the FfFe example).<BR><BR>

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•