• Support
  • Articles
  • Resources
  • Products

Base Conversions

Moderator: boris

1 postPage 1 of 1

IshmaelCallMe
Guru

Posts:
676
Joined: Thu Jan 26, 2006 2:28 am
by IshmaelCallMe » Thu Mar 19, 2009 4:02 pm
These routines will convert a decimal number to a string with that number represented in a different base and back.

Code: Select all

  1.  
  2. basenum='0060E3da'
  3. base=16
  4. call base2dec
  5. int2str sdec decnum
  6. messagebox sdec 'decnum'
  7. base=2
  8. call dec2base
  9. messagebox basenum 'basenum'
  10. end
  11.  
  12. :dec2base   ;converts a decimal number to any base
  13. basenum=''
  14. tmp=decnum ;modified so not destructive of decnum
  15. while tmp>0
  16.     strcopy '0123456789ABCDEF' (tmp%base)+1 1 basedig
  17.     strconcat basedig basenum
  18.     basenum=basedig
  19.     tmp=tmp/base
  20.     endwhile
  21. return
  22.  
  23. :base2dec   ;converts a number in any base to a decimal number
  24. decnum=0
  25. strlen basenum
  26. len=result
  27. for tmp 1 len
  28.     strcopy basenum tmp 1 basedig
  29.     strscan '0123456789ABCDEFabcdef' basedig
  30.     if result=0 break   ;if not a num char, stop conversion
  31.     if result>16 result=result-6 ;take care of lower case
  32.     decnum=decnum*base
  33.     decnum=decnum+result-1
  34. next
  35. return
  36.  

1 postPage 1 of 1

Users browsing this forum: No registered users
cron