We Recommend

ASP.NET 3.5 Unleashed ASP.NET 3.5 Unleashed
ASP.NET 3.5 Unleashed is the most comprehensive book available on the Microsoft ASP.NET 3.5 Framework, covering all aspects of the ASP.NET 3.5 Framework--no matter how advanced.


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. end if
  43.  
  44. ' Return zero if any digit is out of bounds for the radix.
  45. if digit >= radix then
  46. result = 0
  47. exit for
  48. end if
  49.  
  50. result = result + (digit * radix ^ (Len(value) - i))
  51. next
  52.  
  53. toDecimal = result
  54. end function
  55.  
  56. ' Convert from decimal to any base.
  57. function fromDecimal(value, radix)
  58. dim result
  59. dim digit
  60.  
  61. result = ""
  62.  
  63. ' Prevent radix from going out of bounds.
  64. if radix < 2 then
  65. radix = 2
  66. elseif radix > 36 then
  67. radix = 36
  68. end if
  69.  
  70. ' Check for invalid input.
  71. if isNumeric(value) then
  72. ' Inputted value appears to be base 10. OK to proceed.
  73. do until value = 0
  74. digit = value Mod radix
  75.  
  76. if digit > 9 then
  77. digit = Chr(digit + 55)
  78. end if
  79.  
  80. result = CStr(digit) & result
  81. value = value \ radix
  82. loop
  83. else
  84. ' Inputted value was NOT base 10.
  85. result = 0
  86. end if
  87.  
  88. fromDecimal = result
  89. end function
  90.  
  91. ' Convert between bases.
  92. function base_convert(value, sourceRadix, targetRadix)
  93. if sourceRadix = targetRadix then
  94. ' If source radix and target radix are equal, don't waste time converting.
  95. baseConv = value
  96. elseif sourceRadix = 10 then
  97. ' If source radix is decimal, skip converting to decimal.
  98. baseConv = fromDecimal(value, targetRadix)
  99. elseif targetRadix = 10 then
  100. ' If target radix is decimal, skip converting from decimal.
  101. baseConv = toDecimal(value, sourceRadix)
  102. else
  103. ' Convert to decimal, and then from decimal.
  104. baseConv = fromDecimal(toDecimal(value, sourceRadix), targetRadix)
  105. end if
  106. end function
  107.  
  108. ' Convert from binary to decimal.
  109. function bindec(bin)
  110. bindec = toDecimal(bin, 2)
  111. end function
  112.  
  113. ' Convert from decimal to binary
  114. function decbin(dec)
  115. decbin = fromDecimal(dec, 2)
  116. end function
  117.  
  118. ' Convert from decimal to hexadecimal.
  119. function dechex(dec)
  120. ' Assume that built-in hex() function is faster.
  121. dechex = hex(dec)
  122. end function
  123.  
  124. ' Convert from decimal to octal.
  125. function decoct(dec)
  126. ' Assume that built-in oct() function is faster.
  127. decoct = oct(dec)
  128. end function
  129.  
  130. ' Convert from hexadecimal to decimal.
  131. function hexdec(hex)
  132. hexdec = toDecimal(hex, 16)
  133. end function
  134.  
  135. ' Convert from octal to decimal.
  136. function octdec(oct)
  137. octdec = toDecimal(oct, 8)
  138. end function
  139. %>

Report this snippet 

You need to login to post a comment.