Request For Help With Any Base to Any Base Algorithm

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Mar 2008
Posts: 9
Reputation: xRuP7uR3x is an unknown quantity at this point 
Solved Threads: 0
xRuP7uR3x xRuP7uR3x is offline Offline
Newbie Poster

Request For Help With Any Base to Any Base Algorithm

 
0
  #1
Apr 11th, 2008
Greetings everyone. I have been working on a script to convert a number from any base to any base up to base62. The user supplies the original number, the starting base, and the end base. My problem arises when converting the fractional part from base10 to the end base; the script returns an empty string and for the life of me I cannot figure out how to fix it. Any help or advice is greatly appreciated. Code follows.
  1. myinput = raw_input("Number: ")
  2. mystart = int(raw_input("Starting base: "))
  3. myend = int(raw_input("Ending base: "))
  4. intlist = []
  5. fraclist = []
  6. intresult = ""
  7. fracresult = ""
  8. mydict1 = {"A" : 10, "B" : 11, "C" : 12, "D" : 13, "E" : 14, "F" : 15, "G" : 16,
  9. "H" : 17, "I" : 18, "J" : 19, "K" : 20, "L" : 21, "M" : 22, "N" : 23,
  10. "O" : 24, "P" : 25, "Q" : 26, "R" : 27, "S" : 28, "T" : 29, "U" : 30,
  11. "V" : 31, "W" : 32, "X" : 33, "Y" : 34, "Z" : 35, "a" : 36, "b" : 37,
  12. "c" : 38, "d" : 39, "e" : 40, "f" : 41, "g" : 42, "h" : 43, "i" : 44,
  13. "j" : 45, "k" : 46, "l" : 47, "m" : 48, "n" : 49, "o" : 50, "p" : 51,
  14. "q" : 52, "r" : 53, "s" : 54, "t" : 55, "u" : 56, "v" : 57, "w" : 58,
  15. "x" : 59, "y" : 60, "z" : 61}
  16. mydict2 = {10 : "A", 11 : "B", 12 : "C", 13 : "D", 14 : "E", 15 : "F", 16 : "G",
  17. 17 : "H", 18 : "I", 19 : "J", 20 : "K", 21 : "L", 22 : "M", 23 : "N",
  18. 24 : "O", 25 : "P", 26 : "Q", 27 : "R", 28 : "S", 29 : "T", 30 : "U",
  19. 31 : "V", 32 : "W", 33 : "X", 34 : "Y", 35 : "Z", 36 : "a", 37 : "b",
  20. 38 : "c", 39 : "d", 40 : "e", 41 : "f", 42 : "g", 43 : "h", 44 : "i",
  21. 45 : "j", 46 : "k", 47 : "l", 48 : "m", 49 : "n", 50 : "o", 51 : "p",
  22. 52 : "q", 53 : "r", 54 : "s", 55 : "t", 56 : "u", 57 : "v", 58 : "w",
  23. 59 : "x", 60 : "y", 61 : "z"}
  24. #need to create another yes/no loop so user can continue/quit
  25. if myinput.count(".") == 1:
  26. (a, b) = myinput.split(".")
  27. for x in a:
  28. if x in mydict1:
  29. x = mydict1[x]
  30. intlist.append(x)
  31. else:
  32. intlist.append(int(x))
  33. f = 0
  34. g = len(intlist)
  35. while f < (g - 1):
  36. if f == 0:
  37. decint = (intlist[0] * mystart) + intlist[1]
  38. else:
  39. decint = (decint * mystart) + intlist[f + 1]
  40. f = f + 1
  41. while decint > 0:
  42. endint = decint % myend
  43. if endint > 9:
  44. endint = mydict2[endint]
  45. intresult = endint + intresult
  46. else:
  47. intresult = str(endint) + intresult
  48. decint = decint / myend
  49. for x in b:
  50. if x in mydict1:
  51. x = mydict1[x]
  52. fraclist.append(int(x))
  53. else:
  54. fraclist.append(x)
  55. z = 1
  56. decfrac = int(fraclist[-1])
  57. w = len(fraclist)
  58. while z <= w:
  59. if z == w:
  60. n = 0
  61. else:
  62. n = int(fraclist[-1 - z])
  63. decfrac = (decfrac * mystart) + n
  64. z = z + 1
  65. if len(str(decfrac)) > 10:
  66. decfrac = decfrac[:11]
  67. decfrac = decfrac / (10 ** (len(str(decfrac))))
  68. while decfrac < 1 and decfrac > 0:
  69. decfrac = decfrac * myend
  70. s = decfrac // 1
  71. if s > 9:
  72. s = mydict2[s]
  73. fracresult = fracresult + s
  74. else:
  75. fracresult = fracresult + str(s)
  76. decfrac = decfrac - s
  77. elif myinput.count(".") == 0:
  78. for x in myinput:
  79. if x in mydict1:
  80. x = mydict1[x]
  81. intlist.append(x)
  82. else:
  83. intlist.append(int(x))
  84. f = 0
  85. g = len(intlist)
  86. while f < (g - 1):
  87. if f == 0:
  88. decint = (intlist[0] * mystart) + intlist[1]
  89. else:
  90. decint = (decint * mystart) + intlist[f + 1]
  91. f = f + 1
  92. while decint > 0:
  93. endint = decint % myend
  94. if endint > 9:
  95. endint = mydict2[endint]
  96. intresult = endint + intresult
  97. else:
  98. intresult = str(endint) + intresult
  99. decint = decint / myend
  100. else:
  101. print "There is an error in the syntax of your input. The program will now end."
P.S. The variable names for the result strings are intresult and fracresult respectively. Thanks again.
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 608
Reputation: jrcagle is on a distinguished road 
Solved Threads: 150
jrcagle jrcagle is offline Offline
Practically a Master Poster

Re: Request For Help With Any Base to Any Base Algorithm

 
0
  #2
Apr 12th, 2008
Hi. A couple of suggestions.

(1) (minor) mydict2 should be generated dynamically from mydict1 so that if you ever decide to change mydict1, it will automagically change along with it.

Here's how. Replace the entirety of mydict2 with this line:

mydict2 = dict(zip(mydict1.values, mydict1.keys))

This will make a bunch of tuple pairs out of the values and keys from mydict1, and then create a dictionary from them. The order is guaranteed to match according to the Python docs.

(2) Add the digits '0' - '9' to your dictionary. That way, you won't have to have separate conditionals to check for digits v. letters. Remember that both "letters" and "digits" are intended to be just "digits" in whatever base.

(3) You've written code that decodes the number from one base and simultaneously encodes it into another base. That's trying to do too much at once.

Instead, write two separate functions:

from_base(string, base)
to_base(value, base)

from_base will convert the string into a number; to_base will convert the number into a string.

Solving each of those problems is a shorter task and much easier to debug.

(4) The code for converting the fractional part is too complicated. In particular, resorting to the fraclist is not necessary, because can compute the result in place.

I've rewritten that little part below, along with putting in (1) and (2). It will help you greatly to walk through the process of writing functions for (3).

Hope it helps!

  1. myinput = raw_input("Number: ")
  2. mystart = int(raw_input("Starting base: "))
  3. myend = int(raw_input("Ending base: "))
  4. intlist = []
  5. fraclist = []
  6. intresult = ""
  7. fracresult = ""
  8. mydict1 = {"0": 0, "1": 1, "2": 2, "3": 3, "4": 4, "5": 5, "6": 6, "7":7,
  9. "8": 8, "9": 9,
  10. "A" : 10, "B" : 11, "C" : 12, "D" : 13, "E" : 14, "F" : 15, "G" : 16,
  11. "H" : 17, "I" : 18, "J" : 19, "K" : 20, "L" : 21, "M" : 22, "N" : 23,
  12. "O" : 24, "P" : 25, "Q" : 26, "R" : 27, "S" : 28, "T" : 29, "U" : 30,
  13. "V" : 31, "W" : 32, "X" : 33, "Y" : 34, "Z" : 35, "a" : 36, "b" : 37,
  14. "c" : 38, "d" : 39, "e" : 40, "f" : 41, "g" : 42, "h" : 43, "i" : 44,
  15. "j" : 45, "k" : 46, "l" : 47, "m" : 48, "n" : 49, "o" : 50, "p" : 51,
  16. "q" : 52, "r" : 53, "s" : 54, "t" : 55, "u" : 56, "v" : 57, "w" : 58,
  17. "x" : 59, "y" : 60, "z" : 61}
  18. # do this dynamically:
  19.  
  20. mydict2 = dict(zip(mydict1.values(), mydict1.keys()))
  21.  
  22. intresult = fracresult = ""
  23. #need to create another yes/no loop so user can continue/quit
  24. if myinput.count(".") == 1:
  25. (a, b) = myinput.split(".")
  26. for x in a:
  27. if x in mydict1:
  28. x = mydict1[x]
  29. intlist.append(x)
  30. else:
  31. intlist.append(int(x))
  32. f = 0
  33. g = len(intlist)
  34. while f < (g - 1):
  35. if f == 0:
  36. decint = (intlist[0] * mystart) + intlist[1]
  37. else:
  38. decint = (decint * mystart) + intlist[f + 1]
  39. f = f + 1
  40. while decint > 0:
  41. endint = decint % myend
  42. if endint > 9:
  43. endint = mydict2[endint]
  44. intresult = endint + intresult
  45. else:
  46. intresult = str(endint) + intresult
  47. decint = decint / myend
  48.  
  49. fracp = 0.0
  50. placevalue = 1./mystart
  51. for i in range(len(b)): # walk down the digits of b
  52. fracp += mydict1[b] * placevalue
  53. placevalue /= mystart # progressively reduce the placevalue
  54.  
  55. fracresult = ""
  56. while fracp:
  57. fracp *= myend # shift fracp up one place in base myend
  58. remainder = int(fracp)
  59. fracp %= 1 # finds the decimal part of fracp
  60. fracresult += mydict2[remainder]
  61.  
  62. elif myinput.count(".") == 0:
  63. for x in myinput:
  64. if x in mydict1:
  65. x = mydict1[x]
  66. intlist.append(x)
  67. else:
  68. intlist.append(int(x))
  69. f = 0
  70. g = len(intlist)
  71. while f < (g - 1):
  72. if f == 0:
  73. decint = (intlist[0] * mystart) + intlist[1]
  74. else:
  75. decint = (decint * mystart) + intlist[f + 1]
  76. f = f + 1
  77. while decint > 0:
  78. endint = decint % myend
  79. if endint > 9:
  80. endint = mydict2[endint]
  81. intresult = endint + intresult
  82. else:
  83. intresult = str(endint) + intresult
  84. decint = decint / myend
  85. else:
  86. print "There is an error in the syntax of your input. The program will now end."
  87.  
  88. if fracresult:
  89. print intresult + "." + fracresult
  90. elif intresult:
  91. print intresult
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the Python Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC