Re: [Tutor] Doing this in reverse?
On Sat, Sep 13, 2008 at 6:02 PM, Alan Gilfoy [EMAIL PROTECTED]wrote: I found a script at http://code.activestate.com/recipes/65212/ that allows you to convert base 10 numbers to another base. I would like to convert non-base10 numbers to base 10. I wonder if I can do so by flipping the script around a bit: The built-in int() function does what you need - give it a string representation of a number in any base 2-36; pass the base as the second argument. The return is an integer - a pure, Platonic integer - which you can then print in any base you choose. If you omit the second argument (as you usually do), the base is assumed to be 10. The documentation says that if you pass 0 as the second argument, Python will try to guess the base, but it didn't work when I tried it. Example: int('FF', 16) 255 Now, about the code you posted - When you perform integer division, you basically go back to second-grade math (you know, before you learned long division...) The / operator returns the integer quotient - example: 7 / 2 3 The % operator - aka modulo - returns the remainder - example: 7 % 2 1 So... s = while 1: # keep looping until we break out explicitly r = n % base # divide n by base, r is the remainder s = digits[r] + s # look up which digit r corresponds to, add it to the left side of the string we're building n = n / base # divide n by base again, this time keeping only the integer quotient if n == 0: # if quotient is 0, break # we're done -- www.fsrtechnologies.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Doing this in reverse?
On Sun, Sep 14, 2008 at 3:08 AM, Marc Tompkins [EMAIL PROTECTED] wrote: The built-in int() function does what you need - give it a string representation of a number in any base 2-36; pass the base as the second argument. The return is an integer - a pure, Platonic integer - which you can then print in any base you choose. If you omit the second argument (as you usually do), the base is assumed to be 10. The documentation says that if you pass 0 as the second argument, Python will try to guess the base, but it didn't work when I tried it. The docs say, If radix is zero, the proper radix is guessed based on the contents of string; the interpretation is the same as for integer literals. 'guessed' is not really a very good choice of words; 'determined' might be better. The last part of the sentence is the clue. With one argument, int() expects just the string representation of an int; any characters that are not digits are an error. For example: In [7]: int('0xaa') --- ValueErrorTraceback (most recent call last) /Users/kent/ipython console in module() ValueError: invalid literal for int() with base 10: '0xaa' If you pass 0 as the second argument, int() interprets the usual prefix for integer literals, and the above works: In [4]: int('0xaa', 0) Out[4]: 170 Also notice the difference here: In [5]: int('011') Out[5]: 11 In [6]: int('011', 0) Out[6]: 9 In the first case, the leading 0 has no special meaning and the result is 11. In the second case, the leading 0 is interpreted to mean 'octal' and the result is different. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Doing this in reverse?
On Sun, Sep 14, 2008 at 4:48 AM, Kent Johnson [EMAIL PROTECTED] wrote: The docs say, If radix is zero, the proper radix is guessed based on the contents of string; the interpretation is the same as for integer literals. 'guessed' is not really a very good choice of words; 'determined' might be better. The last part of the sentence is the clue. You're right - if it had said determined, I would have thought to insert a 0x at the beginning of my test string. Since it said guessed, it didn't occur to me to do that, with the following result: int(FF, 0) Traceback (most recent call last): File input, line 1, in module ValueError: invalid literal for int() with base 0: 'FF' which seemed to say to me that the function is trying to interpret the string as base 0: what the heck would that mean, anyway? Is that not just a particularly elaborate version of division by zero? I had written a short passage reviling this as a useless feature, when it struck me that there actually is a use case for it: if you're parsing a text file (or perhaps the Windows registry?) full of numeric values in arbitrary encodings. You know - on one line there's a value expressed in decimal, then something in octal, something in binary, something in hex. If I ever run into a file like that - and so far I never have - I won't have to write a custom parser. Hurrah! -- www.fsrtechnologies.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Doing this in reverse?
On Sun, Sep 14, 2008 at 3:34 PM, Marc Tompkins [EMAIL PROTECTED] wrote: On Sun, Sep 14, 2008 at 4:48 AM, Kent Johnson [EMAIL PROTECTED] wrote: 'guessed' is not really a very good choice of words; 'determined' might be better. The last part of the sentence is the clue. You're right - if it had said determined, I would have thought to insert a 0x at the beginning of my test string. BTW I submitted a bug report suggesting this change and it has been patched already :-) Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Doing this in reverse?
On Sun, Sep 14, 2008 at 2:29 PM, Kent Johnson [EMAIL PROTECTED] wrote: On Sun, Sep 14, 2008 at 3:34 PM, Marc Tompkins [EMAIL PROTECTED] wrote: On Sun, Sep 14, 2008 at 4:48 AM, Kent Johnson [EMAIL PROTECTED] wrote: 'guessed' is not really a very good choice of words; 'determined' might be better. The last part of the sentence is the clue. You're right - if it had said determined, I would have thought to insert a 0x at the beginning of my test string. BTW I submitted a bug report suggesting this change and it has been patched already :-) Kent Which - the documentation, or the function? -- www.fsrtechnologies.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor