Posted By

Scooter on 05/19/08


Tagged

math


Versions (?)

Base Conversion


 / 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.

  1. <%
  2. ' ASP Mathematics Library - Base Conversion
  3. '
  4. ' Copyright (c) 2008, reusablecode.blogspot.com; some rights reserved.
  5. '
  6. ' This work is licensed under the Creative Commons Attribution License. To view
  7. ' a copy of this license, visit http://creativecommons.org/licenses/by/3.0/ or
  8. ' send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California
  9. ' 94305, USA.
  10.  
  11. ' These functions are only capable of converting to and from bases between 2 and 36.
  12. ' I decided not to support larger bases because the PHP equivalent base_convert function is also limited to base 36.
  13.  
  14. ' From base 37 to 62, the lowercase letters of the Latin alphabet are used, but special handling would need to be added for them.
  15. ' The ASCII value for Z is 90. The ASCII value for a is 97. There are six other characters in between.
  16.  
  17. ' Furthermore, Base64 encoding begins with the uppercase letters A-Z, followed by a-z, followed by 0-9, followed by + and /.
  18. ' If you want to add Base64 support, separate functions might be wise, but could still be tied into the base_convert wrapper.
  19.  
  20. ' Convert from any base to decimal.
  21. function toDecimal(value, radix)
  22. dim result
  23. dim digit
  24.  
  25. result = 0
  26.  
  27. ' Prevent radix from going out of bounds.
  28. if radix < 2 then
  29. radix = 2
  30. elseif radix > 36 then
  31. radix = 36
  32. end if
  33.  
  34. for i = 1 to Len(value)
  35. digit = Mid(value, i, 1)
  36.  
  37. ' Convert letters to numbers.
  38. if not isNumeric(digit) then
  39. ' The letter A in any base is equal to 10 in decimal.
  40. ' The ASCII value of A is 65, so subtract 55 from the ASCII value to obtain the decimal value.
  41. digit = Asc(UCase(digit)) - 55
  42. else
  43. digit = CInt(digit)
  44. end if
  45.  
  46. ' Return zero if any digit is out of bounds for the radix.
  47. if digit >= radix then
  48. result = 0
  49. exit for
  50. end if
  51.  
  52. result = result + (digit * radix ^ (Len(value) - i))
  53. next
  54.  
  55. toDecimal = result
  56. end function
  57.  
  58. ' Convert from decimal to any base.
  59. function fromDecimal(value, radix)
  60. dim result
  61. dim digit
  62.  
  63. result = ""
  64.  
  65. ' Prevent radix from going out of bounds.
  66. if radix < 2 then
  67. radix = 2
  68. elseif radix > 36 then
  69. radix = 36
  70. end if
  71.  
  72. ' Check for invalid input.
  73. if isNumeric(value) then
  74. ' Inputted value appears to be base 10. OK to proceed.
  75. do until value = 0
  76. digit = value Mod radix
  77.  
  78. if digit > 9 then
  79. digit = Chr(digit + 55)
  80. end if
  81.  
  82. result = CStr(digit) & result
  83. value = value \ radix
  84. loop
  85. else
  86. ' Inputted value was NOT base 10.
  87. result = 0
  88. end if
  89.  
  90. fromDecimal = result
  91. end function
  92.  
  93. ' Convert between bases.
  94. function base_convert(value, sourceRadix, targetRadix)
  95. if sourceRadix = targetRadix then
  96. ' If source radix and target radix are equal, don't waste time converting.
  97. baseConv = value
  98. elseif sourceRadix = 10 then
  99. ' If source radix is decimal, skip converting to decimal.
  100. baseConv = fromDecimal(value, targetRadix)
  101. elseif targetRadix = 10 then
  102. ' If target radix is decimal, skip converting from decimal.
  103. baseConv = toDecimal(value, sourceRadix)
  104. else
  105. ' Convert to decimal, and then from decimal.
  106. baseConv = fromDecimal(toDecimal(value, sourceRadix), targetRadix)
  107. end if
  108. end function
  109.  
  110. ' Convert from binary to decimal.
  111. function bindec(bin)
  112. bindec = toDecimal(bin, 2)
  113. end function
  114.  
  115. ' Convert from decimal to binary
  116. function decbin(dec)
  117. decbin = fromDecimal(dec, 2)
  118. end function
  119.  
  120. ' Convert from decimal to hexadecimal.
  121. function dechex(dec)
  122. ' Assume that built-in hex() function is faster.
  123. dechex = hex(dec)
  124. end function
  125.  
  126. ' Convert from decimal to octal.
  127. function decoct(dec)
  128. ' Assume that built-in oct() function is faster.
  129. decoct = oct(dec)
  130. end function
  131.  
  132. ' Convert from hexadecimal to decimal.
  133. function hexdec(hex)
  134. hexdec = toDecimal(hex, 16)
  135. end function
  136.  
  137. ' Convert from octal to decimal.
  138. function octdec(oct)
  139. octdec = toDecimal(oct, 8)
  140. end function
  141. %>

Report this snippet  

You need to login to post a comment.