home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / OL.LZH / PROCS.LZH / RADCON.ICN < prev    next >
Text File  |  1991-07-13  |  1KB  |  58 lines

  1. ############################################################################
  2. #
  3. #    Name:    radcon.icn
  4. #
  5. #    Title:    Radix conversion
  6. #
  7. #    Author:    Ralph E. Griswold
  8. #
  9. #    Date:    June 10, 1988
  10. #
  11. ############################################################################
  12. #  
  13. #  The following procedures convert numbers from one radix to
  14. #  another. The letters from a to z are used for ``digits'' greater
  15. #  than 9. All the conversion procedures fail if the conversion can-
  16. #  not be made.
  17. #  
  18. #       exbase10(i,j)  convert base-10 integer i to base j
  19. #  
  20. #       inbase10(s,i)  convert base-i integer s to base 10
  21. #  
  22. #       radcon(s,i,j)  convert base-i integer s to base j
  23. #  
  24. #  Limitation:
  25. #
  26. #     The maximum base allowed is 36.
  27. #  
  28. ############################################################################
  29.  
  30. procedure exbase10(i,j)
  31.    static digits
  32.    local s, d, sign
  33.    initial digits := &digits || &lcase
  34.    if i = 0 then return 0
  35.    if i < 0 then {
  36.       sign := "-"
  37.       i := -i
  38.       }
  39.    else sign := ""
  40.    s := ""
  41.    while i > 0 do {
  42.       d := i % j
  43.       if d > 9 then d := digits[d + 1]
  44.       s := d || s
  45.       i /:= j
  46.       }
  47.    return sign || s
  48. end
  49.  
  50. procedure inbase10(s,i)
  51.    if s[1] == "-" then return "-" || integer(i || "r" || s[2:0])
  52.    else return integer(i || "r" || s)
  53. end
  54.  
  55. procedure radcon(s,i,j)
  56.    return exbase10(inbase10(s,i),j)
  57. end
  58.