nowox
nowox

Reputation: 29096

How to convert any numeric string value in int?

I would like to convert either 0x123, 012 or 36#foo into an integer.

To do so, I wrote this:

def str2int(s):
    if re.findall('(?i)^0x',s):
        return int(s, 16)
    if re.findall('^(?i)0',s):
        return int(s, 8)
    m = re.findall('(?i)^(\d+)\#([0-9a-z]+)',s)
    if m:
        return int(m[0][1], m[0][0])
    raise AssertionError, 'Unknown value'

I feel it is a bit complicated. Is there any builtin method?

Upvotes: 0

Views: 89

Answers (3)

poke
poke

Reputation: 387607

Solution without regular expressions:

def convert (s):
    if s.lower().startswith('0x'):
        s = '16#' + s[2:]
    elif s.startswith('0'):
        s = '8#' + s[1:]
    elif '#' not in s:
        s = '10#' + s
    base, num = s.split('#', 1)
    return int(num, int(base))
>>> testcases = [('0x123', 291), ('012', 10), ('36#foo', 20328)]
>>> for s, n in testcases:
        print(s, n, n == convert(s))

0x123 291 True
012 10 True
36#foo 20328 True

Upvotes: 2

GingerPlusPlus
GingerPlusPlus

Reputation: 5606

int will do, when you pass 0 as second argument:

   int('0x123', 0)
=> 291
   int('0o12', 0)
=> 10

If you want to support comments, str.partition is the simplest way I can think about:

   int('36#foo'.partition('#')[0], 0)
=> 36

Upvotes: 1

Kevin
Kevin

Reputation: 76194

Yes, you can use ast.literal_eval.

>>> import ast
>>> ast.literal_eval("0x123")
291
>>> ast.literal_eval("012")
10
>>> ast.literal_eval("36#foo")
36

However, note that literal_eval("012") will only work in 2.7 and lower because 3.x no longer supports that style of octal literal. But this will work:

>>> ast.literal_eval("0o12")
10

Upvotes: 2

Related Questions