How to restrict the base when using string.atoi() in converting string to number in base?

Alex Martelli aleaxit at yahoo.com
Tue Apr 17 08:38:27 EDT 2001


"EdwardT" <edwardt at trillium.com> wrote in message
news:9bg00u$cln at news.or.intel.com...
> Hi I have a funciton that converts number written in hex to numerical
value.
>
> I use code like:
>
> string.atoi(inputString,16) to do it.
>
> For example:
>
> 0X06
>
> shall be converted to 6.
>
> But I want to restrict user from using 0A06, 0a08 etc; only allows the
> decimal case or the hex case, ie. 6,06,006, 0X06, 0x06 shall all be
allowed

I'm not too sure of exactly what you want, but, what about seeing
if the input strings starts with a '0x' (either case) and doing
decimal or base-16 translation depending on that?  If you need to
tolerate leading space, you may want to use a regular expression
(but, it can be done with just string-manipulation).


Alex







More information about the Python-list mailing list