I have a string that can be a hex number prefixed with "0x" or a decimal number without a special prefix except for possibly a minus sign. "0x123" is in base 16 and "-298" is in base 10.
How do I convert this to an int or long in Python?
I don't want to use eval() since it's unsafe and overkill.
int(..., 0)solution, that's not what the OP asks for (for example with string"10"this should return 10, the other question should return 16) -- in fact I'd say that theint(x,0)method is wrong for the other question (for the reason above)