By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,686 Members | 1,603 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,686 IT Pros & Developers. It's quick & easy.

Convert decimal to 36-base system

P: 10
Hi,

Can anyone help me?
I need a code in VBA which would convert decimal numbers to the other system bases on number 36.
e.g. in this page http://www.translatorscafe.com/cafe/units-converter/numbers/calculator/octal-to-decimal/
decimal to base-36.
Can anyone help me with that?
May 12 '11 #1
Share this Question
Share on Google+
4 Replies


Rabbit
Expert Mod 10K+
P: 12,374
First you find the highest power of 36 that isn't larger than the number you're trying to convert. Then you divide by that. The whole number from that result gives you the leftmost digit. You take that whole number, multiply by the power of 36 and subtract it from the number you're converting. Then you repeat to get the rest of the digits.

Expand|Select|Wrap|Line Numbers
  1. 17(base 10) -> (base 16)
  2.  
  3. 16^0 = 1
  4. 16^1 = 16
  5. 16^2 = 256
  6.  
  7. 17 / 16^1 = 1.0625
  8. 1 is the leftmost digit in base 16
  9.  
  10. 17 - 1 * 16^1 = 1
  11. 1 / 16^0 = 1
  12.  
  13. 17 (Base 10) = 11 (Base 16)
May 12 '11 #2

P: 10
Thanks for the answer. The algorithm is clear. Another question: Perhaps there is a function in excel which converts from base 10 to base 36? This function is opposite. It converts numbers from base 36 to base 10:
Expand|Select|Wrap|Line Numbers
  1. =IF(A1="";"0";SUMPRODUCT(POWER(36;LEN(A1)-ROW(INDIRECT("1:"&LEN(A1))));(CODE(UPPER(MID(A1;ROW(INDIRECT("1:"&LEN(A1)));1)))-48*(CODE(MID(A1;ROW(INDIRECT("1:"&LEN(A1)));1))<58)-55*(CODE(MID(A1;ROW(INDIRECT("1:"&LEN(A1)));1))>64))))
May 12 '11 #3

Rabbit
Expert Mod 10K+
P: 12,374
There's no native function if that's what you're asking.
May 12 '11 #4

ADezii
Expert 5K+
P: 8,668
Here is a little Function that will convert any Positive Integer to a Base36 String:
Expand|Select|Wrap|Line Numbers
  1. Function fBase36Encode(ByRef lngNumToConvert As Long) As String
  2. 'Will Convert any Positive Integer to a Base36 String
  3. Dim strAlphabet As String
  4.  
  5. strAlphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  6.  
  7. If lngNumToConvert = 0 Then
  8.   Base36Encode = "0"
  9.     Exit Function
  10. End If
  11.  
  12. fBase36Encode = vbNullString
  13.  
  14. Do While lngNumToConvert <> 0
  15.   fBase36Encode = Mid(strAlphabet, lngNumToConvert Mod 36 + 1, 1) & fBase36Encode
  16.     lngNumToConvert = lngNumToConvert \ 36
  17. Loop
  18. End Function
May 13 '11 #5

Post your reply

Sign in to post your reply or Sign up for a free account.