Home

------=_Part_41077_28921141.1186960849376
Content-Type: multipart/alternative;
boundary="----=_Part_41078_8128191.1186960849376"

------=_Part_41078_8128191.1186960849376
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

Well, I decided to implement my own way of doing this. I've attached the
source. You're all welcome :)

On 8/12/07, Michael Bentley <michael@jedimindworks.com> wrote:
>
> Hi Robert,
> On Aug 11, 2007, at 3:59 PM, Robert Dailey wrote:
>
> Hi, I was wondering if there is a built in module that supports conversion
> in any direction between Binary, Hex, and Decimal strings? Thanks.
>
>
> Shouldn't be too hard to build one. Here's a little incantation to
> convert from base 10 to another base:
>
> import string
>
> def to_base(number, base):
> 'converts base 10 integer to another base'
>
> number = int(number)
> base = int(base)
> if base < 2 or base > 36:
> raise ValueError, "Base must be between 2 and 36"
> if not number:
> return 0
> symbols = string.digits + string.lowercase[:26]
> answer = []
> while number:
> number, remainder = divmod(number, base)
> answer.append(symbols[remainder])
> return ''.join(reversed(answer))
>
> How 'bout you hack a from_base function and email it back to me? (hint:
> type 'help(int)' in the python interpreter).
>
> Peace,
> Michael
>
> ---
> Let the wookie win.
>
>
>
>

------=_Part_41078_8128191.1186960849376
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

Well, I decided to implement my own way of doing this. I&#39;ve attached the source. You&#39;re all welcome :)<br><br><div><span class="gmail_quote">On 8/12/07, <b class="gmail_sendername">Michael Bentley</b> &lt;<a href="mailto:michael@jedimindworks.com">
michael@jedimindworks.com</a>&gt; wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><div style="">Hi Robert,<div><span class="q">
<br><div><div>On Aug 11, 2007, at 3:59 PM, Robert Dailey wrote:</div><blockquote type="cite">Hi, I was wondering if there is a built in module that supports conversion in any direction between Binary, Hex, and Decimal strings? Thanks.
</blockquote></div><br></span><div><span style="border-collapse: separate; border-spacing: 0px; color: rgb(0, 0, 0); font-family: Monaco; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;">
Shouldn&#39;t be too hard to build one.&nbsp; Here&#39;s a little incantation to convert from base 10 to another base:</span></div><div><br></div><div>import string</div><div><br></div><div>def to_base(number, base):</div><div>
<span style="white-space: pre;"> </span>&#39;converts base 10 integer to another base&#39;</div><div><br></div><div><span style="white-space: pre;"> </span>number = int(number)</div><div><span style="white-space: pre;"> </span>
base = int(base)<span style="white-space: pre;"> </span></div><div><span style="white-space: pre;"> </span>if base &lt; 2 or base &gt; 36:</div><div><span style="white-space: pre;"> </span>raise ValueError, &quot;Base must be between 2 and 36&quot;
<span style="white-space: pre;"> </span></div><div><span style="white-space: pre;"> </span>if not number:</div><div><span style="white-space: pre;"> </span>return 0</div><div><span style="white-space: pre;"> </span></div>
<div><span style="white-space: pre;"> </span>symbols = string.digits + string.lowercase[:26]<span style="white-space: pre;"> </span></div><div><span style="white-space: pre;"> </span>answer = []</div><div><span style="white-space: pre;">
</span>while number:</div><div><span style="white-space: pre;"> </span>number, remainder = divmod(number, base)</div><div><span style="white-space: pre;"> </span>answer.append(symbols[remainder])<span style="white-space: pre;">
</span></div><div><span style="white-space: pre;"> </span>return &#39;&#39;.join(reversed(answer))</div><div><span style="border-collapse: separate; border-spacing: 0px; color: rgb(0, 0, 0); font-family: Monaco; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;">
<span style="border-collapse: separate; border-spacing: 0px; color: rgb(0, 0, 0); font-family: Monaco; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;">
<div><br></div><div>How &#39;bout you hack a from_base function and email it back to me? (hint: type &#39;help(int)&#39; in the python interpreter).</div><div><br></div><div>Peace,</div><div >Michael</div><div><br></div><div>
---</div><div>Let the wookie win.</div><div><br></div><br></span></span> </div><br></div></div></blockquote></div><br>

------=_Part_41078_8128191.1186960849376--

------=_Part_41077_28921141.1186960849376
Content-Type: text/plain; name="baseconv.py"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="baseconv.py"
X-Attachment-Id: f_f5a5roa4

DQojIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMj IyMjIyMjIyMjIyMj
IyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjDQoNCmRlZiBoZXgyZGVjKCBoZXgg KToNCglyZXR1cm4g
c3RyKCBpbnQoIGhleCwgMTYgKSApDQoNCiMjIyMjIyMjIyMjIyMjIyMjIyMj IyMjIyMjIyMjIyMj
IyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMj IyMNCg0KZGVmIGhl
eDJiaW4oIGhleCApOg0KDQoJbmliYmxlcyA9CXsNCgkJCQkiMCI6IjAwMDAi LCAiMSI6IjAwMDEi
LCAiMiI6IjAwMTAiLCAiMyI6IjAwMTEiLA0KCQkJCSI0IjoiMDEwMCIsICI1 IjoiMDEwMSIsICI2
IjoiMDExMCIsICI3IjoiMDExMSIsDQoJCQkJIjgiOiIxMDAwIiwgIjkiOiIx MDAxIiwgIkEiOiIx
MDEwIiwgIkIiOiIxMDExIiwNCgkJCQkiQyI6IjExMDAiLCAiRCI6IjExMDEi LCAiRSI6IjExMTAi
LCAiRiI6IjExMTEiDQoJCQkJfQ0KDQoJIyBDaGVjayBmb3IgJzB4JyBpbiBm cm9udCBvZiB0aGUg
c3RyaW5nICYgcmVtb3ZlIGl0DQoJaWYgaGV4WzoyXSA9PSAiMHgiOg0KCQlo ZXggPSBoZXhbMjpd
DQoNCgl0cnk6DQoJCXJldHVybiBzdHIoKS5qb2luKFtuaWJibGVzW2NdIGZv ciBjIGluIGhleC51
cHB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
------=_Part_41077_28921141.1186960849376--

previous
next

Re: Biased random?
Re: The meaning of a = b in object oriented languages
Re: Online free courses in C++?
Re: can c++ deal with hardware???
Re: Test-driven development and code size
Nasze Dzieci
Krwinka
Fundacja Hobbit
Mam Marzenie
Fundacja Sloneczko