mnaberez / py65

Emulate 6502-based microcomputer systems in Python
BSD 3-Clause "New" or "Revised" License
234 stars 68 forks source link

Rewrite of itoa() in utils/conversions.py #36

Closed scotws closed 7 years ago

scotws commented 7 years ago

The current version of conversions.py will convert a number to any base, though py65 only uses binary, hex, and decimal (see "Number Systems" in the docs). Also, this routine will not produce a warning if for some reason the base is given as, say, 17.

The changes replace this code with str.format() commands, making use of the faster built-in functions, simplifying the code to a single if-test, and throw a ValueError if the base is not binary, hex, or decimal. Hexadecimal characters are lowercase as in the original routine.

The changes pass the included tests called as

 python3 -m unittest py65/tests/utils/test_conversions.py

with Python 3.5.2 on Ubuntu 16.04.3 LTS. As I am unsure of the procedure here, I'd be grateful for further testing by somebody who, uh, actually knows what they are doing.