/ Published in: ASP
URL: http://reusablecode.blogspot.com/2008/05/base-conversion.html
PHP's base conversion functions ported to ASP. Limited to base 36, but could be extended beyond that. See corresponding blog spot and code comments for more information on what would need to be done.
Expand |
Embed | Plain Text
<% ' ASP Mathematics Library - Base Conversion ' ' Copyright (c) 2008, reusablecode.blogspot.com; some rights reserved. ' ' This work is licensed under the Creative Commons Attribution License. To view ' a copy of this license, visit http://creativecommons.org/licenses/by/3.0/ or ' send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California ' 94305, USA. ' These functions are only capable of converting to and from bases between 2 and 36. ' I decided not to support larger bases because the PHP equivalent base_convert function is also limited to base 36. ' From base 37 to 62, the lowercase letters of the Latin alphabet are used, but special handling would need to be added for them. ' The ASCII value for Z is 90. The ASCII value for a is 97. There are six other characters in between. ' Furthermore, Base64 encoding begins with the uppercase letters A-Z, followed by a-z, followed by 0-9, followed by + and /. ' If you want to add Base64 support, separate functions might be wise, but could still be tied into the base_convert wrapper. ' Convert from any base to decimal. function toDecimal(value, radix) dim result dim digit result = 0 ' Prevent radix from going out of bounds. if radix < 2 then radix = 2 elseif radix > 36 then radix = 36 end if for i = 1 to Len(value) digit = Mid(value, i, 1) ' Convert letters to numbers. if not isNumeric(digit) then ' The letter A in any base is equal to 10 in decimal. ' The ASCII value of A is 65, so subtract 55 from the ASCII value to obtain the decimal value. digit = Asc(UCase(digit)) - 55 else digit = CInt(digit) end if ' Return zero if any digit is out of bounds for the radix. if digit >= radix then result = 0 exit for end if result = result + (digit * radix ^ (Len(value) - i)) next toDecimal = result end function ' Convert from decimal to any base. function fromDecimal(value, radix) dim result dim digit result = "" ' Prevent radix from going out of bounds. if radix < 2 then radix = 2 elseif radix > 36 then radix = 36 end if ' Check for invalid input. if isNumeric(value) then ' Inputted value appears to be base 10. OK to proceed. do until value = 0 digit = value Mod radix if digit > 9 then digit = Chr(digit + 55) end if result = CStr(digit) & result value = value \ radix loop else ' Inputted value was NOT base 10. result = 0 end if fromDecimal = result end function ' Convert between bases. function base_convert(value, sourceRadix, targetRadix) if sourceRadix = targetRadix then ' If source radix and target radix are equal, don't waste time converting. baseConv = value elseif sourceRadix = 10 then ' If source radix is decimal, skip converting to decimal. baseConv = fromDecimal(value, targetRadix) elseif targetRadix = 10 then ' If target radix is decimal, skip converting from decimal. baseConv = toDecimal(value, sourceRadix) else ' Convert to decimal, and then from decimal. baseConv = fromDecimal(toDecimal(value, sourceRadix), targetRadix) end if end function ' Convert from binary to decimal. function bindec(bin) bindec = toDecimal(bin, 2) end function ' Convert from decimal to binary function decbin(dec) decbin = fromDecimal(dec, 2) end function ' Convert from decimal to hexadecimal. function dechex(dec) ' Assume that built-in hex() function is faster. dechex = hex(dec) end function ' Convert from decimal to octal. function decoct(dec) ' Assume that built-in oct() function is faster. decoct = oct(dec) end function ' Convert from hexadecimal to decimal. function hexdec(hex) hexdec = toDecimal(hex, 16) end function ' Convert from octal to decimal. function octdec(oct) octdec = toDecimal(oct, 8) end function %>
You need to login to post a comment.
