Saturday, May 3, 2008

Base Conversion

Most of the modern world uses a base-10 number system. It's the easiest to work with. However, when it comes to computer systems, we use a few different number systems too. Base-2, or binary, is the basis of data storage and transmission; a bit is either on or off. We also use Base-8, or octal; each byte contains 8 bits. Last but not least is Base-16, or hexadecimal.


Under some circumstances, we might need to convert between these different number systems. ASP provides a pathetic two functions for this purpose: hex(), which converts from base-10 (decimal) to base-16 (hexadecimal), and oct() which converts from decimal to octal. PHP provides equivalent functions dechex() and decoct(), as well as additional functions decbin() for converting from decimal to binary, bindec() for converting from binary to decimal, hexdec() for converting from hexadecimal to decimal, and octdec() for converting from octal to decimal.


It would be nice to extend ASP to support these four additional functions. When I started doing that, I noticed a pattern in the formulae. All the functions that converted from decimal were fairly similar, and all the functions that converted to decimal were also fairly similar. So I abstracted the calculations out into their own functions and simplified my code.


ASP

  1. ' Convert from binary to decimal.
  2. function bindec(bin)
  3.     bindec = toDecimal(bin, 2)
  4. end function
  5. ' Convert from decimal to binary
  6. function decbin(dec)
  7.     decbin = fromDecimal(dec, 2)
  8. end function
  9. ' Convert from decimal to hexadecimal.
  10. function dechex(dec)
  11.     ' Assume that built-in hex() function is faster.
  12.     dechex = hex(dec)
  13. end function
  14. ' Convert from decimal to octal.
  15. function decoct(dec)
  16.     ' Assume that built-in oct() function is faster.
  17.     decoct = oct(dec)
  18. end function
  19. ' Convert from hexadecimal to decimal.
  20. function hexdec(hex)
  21.     hexdec = toDecimal(hex, 16)
  22. end function
  23. ' Convert from octal to decimal.
  24. function octdec(oct)
  25.     octdec = toDecimal(oct, 8)
  26. end function

Before we get into the underlying fromDecimal() and toDecimal() functions, some things need to be said. The lowest possible base is 2, so if the user tries to specify a base smaller than 2, we will change it to 2. Likewise, there is a practical upper limit of base 36. Beyond that, things get more complicated, so any base higher than 36 will be changed to 36 to prevent the function from going out of bounds and returning bad data.


ASP

  1. function toDecimal(value, radix)
  2.     dim result
  3.     dim digit
  4.     result = 0
  5.     ' Prevent radix from going out of bounds.
  6.     if radix < 2 then
  7.         radix = 2
  8.     elseif radix > 36 then
  9.         radix = 36
  10.     end if
  11.     for i = 1 to Len(value)
  12.         digit = Mid(value, i, 1)
  13.         ' Convert letters to numbers.
  14.         if not isNumeric(digit) then
  15.             ' The letter A in any base is equal to 10 in decimal.
  16.             ' The ASCII value of A is 65, so subtract 55 from the ASCII value to obtain the decimal value.
  17.             digit = Asc(UCase(digit)) - 55
  18.         else
  19.             digit = CInt(digit)
  20.         end if
  21.         ' Return zero if any digit is out of bounds for the radix.
  22.         if digit >= radix then
  23.             result = 0
  24.             exit for
  25.         end if
  26.         result = result + (digit * radix ^ (Len(value) - i))
  27.     next
  28.     toDecimal = result
  29. end function
  30. function fromDecimal(value, radix)
  31.     dim result
  32.     dim digit
  33.     result = 0
  34.     ' Prevent radix from going out of bounds.
  35.     if radix < 2 then
  36.         radix = 2
  37.     elseif radix > 36 then
  38.         radix = 36
  39.     end if
  40.     ' Check for invalid input.
  41.     if isNumeric(value) then
  42.         ' Inputted value appears to be base 10. OK to proceed.
  43.         do until value = 0
  44.             digit = value Mod radix
  45.             if digit > 9 then
  46.                 digit = Chr(digit + 55)
  47.             end if
  48.             result = CStr(digit) & result
  49.             value = value \ radix
  50.         loop
  51.     else
  52.         ' Inputted value was NOT base 10.
  53.         result = 0
  54.     end if
  55.     fromDecimal = result
  56. end function

It would be even better if we could easily convert between two bases where neither one is decimal. Also, it would be nice to have a unified interface. We can achieve both with one function, base_convert(), which is also present in PHP.


ASP

  1. function base_convert(value, sourceRadix, targetRadix)
  2.     if sourceRadix = targetRadix then
  3.         ' If source radix and target radix are equal, don't waste time converting.
  4.         baseConv = value
  5.     elseif sourceRadix = 10 then
  6.         ' If source radix is decimal, skip converting to decimal.
  7.         baseConv = fromDecimal(value, targetRadix)
  8.     elseif targetRadix = 10 then
  9.         ' If target radix is decimal, skip converting from decimal.
  10.         baseConv = toDecimal(value, sourceRadix)
  11.     else
  12.         ' Convert to decimal, and then from decimal.
  13.         baseConv = fromDecimal(toDecimal(value, sourceRadix), targetRadix)
  14.     end if
  15. end function

I leave you with the following potential exercises:

  • 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.
  • 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 would be wise, but could still be tied into the base_convert() wrapper.

2 comments:

Unknown said...

There is a bug in the toDecimal function - try caling toDecimal("8A", 36) and get 0 back.

The issue is you do not convert a numeric digit to an Integer, so VBScript does a string comparison and "8" is greater than "36".

I suggest adding an else to the if not isnumeric:
else
digit = CInt(digit)

Scott said...

Thanks, I've updated the code both here and on Snipplr.