+ 1
Can someone tell me how to check whether the number is binary or hex or decimal in python
8 Réponses
+ 3
You're welcome! glad to help : )
+ 1
Convert number to string first. Decimal contains 0-9 only e.g. 16, hex literals begins with 0x (zero followed by x, e.g. 0x10), octal literals begins with 0o (zero followed by o, e.g. 0o20), and binary literals begins with 0b (zero followed by b, e.g. 0b10000). Check for this prefixes, then check each character following the prefix, whether they are valid digit for the aforementioned numeral base.
* Decimal: 0 ~ 9, - (negative sign)
* Hexadecimal: 0 ~ 9, A ~ F, a ~ f
* Octal: 0 ~ 7
* Binary: 0 ~ 1
Any presence of digit out of valid range indicates it's not a valid number in a certain base.
Hth, cmiiw
+ 1
prateek verma Sorry for delay, the idea is to check the first two characters in a string, if it matches a certain numeral base prefix we then check each character after the prefix, whether or not the character is within valid range of characters allowed for the said numeral base. If no numeral base prefix available we can use int() function to test, if the string can be converted to integer (example follows)
Note that we cannot rely on numeral base prefix alone, because Python can work with numeric string without prefix at all.
"0b10000000"
"10000000"
Above ↑ are string of binary number (base 2), the first has base indicator (the '0b' prefix), the second doesn't.
"0o200"
"200"
Above ↑ are string of octal number (base 8), the first has base indicator (the '0o' prefix), the second doesn't.
"0x80"
"80"
Above ↑ are string of hexadecimal number (base 16), the first has base indicator (the '0x' prefix), the second doesn't.
* Continued on next post.
+ 1
For binary, allowed digits are 0 and 1. If any character in the string isn't either 0 or 1 (except for the prefix; if any), it is not valid binary number.
For octal, allowed digits are 0, 1, 2 ,3 ,4 ,5 ,6 ,7. If any character in the string isn't within that range (except for the prefix; if any), it is not valid octal number.
For hexadecimal, allowed digits are 0, 1, 2 ,3 ,4 ,5 ,6 ,7, 8, 9, a, b, c ,d, e, f. If any character in the string isn't within that range (except for the prefix), it is not valid hexadecimal number.
* Continued on next post
+ 1
Well now, I wrote these for you, you can use them for checking whether a numeric string is of a certain base.
def IsNumericBase(s, base):
try:
v = int(s, base)
return True
except ValueError:
return False
# Returns True if <s> is binary number string
def IsBinaryString(s):
return IsNumericBase(s, 2)
# Returns True if <s> is octal number string
def IsOctalString(s):
return IsNumericBase(s, 8)
# Returns True if <s> is hexadecimal number string
def IsHexadecimalString(s):
return IsNumericBase(s, 16)
print(IsBinaryString("10000000"))
print(IsBinaryString("0b10000000"))
print(IsOctalString ("200"))
print(IsOctalString ("0o200"))
print(IsHexadecimalString("80"))
print(IsHexadecimalString("0x80"))
+ 1
thank you Ipang
0
Ipang plz look at my code.. plz correct me