figuring out if a number is decimal, hexidecimal, or octal

• (2 Pages)
• 1
• 2

29 Replies - 10832 Views - Last Post: 18 June 2013 - 11:42 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=322927&amp;s=45a53832052e9067fa974365eb9eab87&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#16 blindchicken11

Reputation: 0
• Posts: 93
• Joined: 19-September 12

Re: figuring out if a number is decimal, hexidecimal, or octal

Posted 13 June 2013 - 12:20 AM

Hmm, so I wouldn't have to split the number up into separate characters in a list then? I could just save it as a string, then compare that using the match function?

P.S. Thank you for the replies, I appreciate it

#17 Mekire

Reputation: 118
• Posts: 216
• Joined: 11-January 13

Re: figuring out if a number is decimal, hexidecimal, or octal

Posted 13 June 2013 - 01:10 AM

There shouldn't be any need for that. Also if you do ever have a need to do that, it is as simple as:
```>>> mystring = "Hello world!"
>>> list(mystring)
['H', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', '!']
>>>
```

This should match any hex number, at least in the format I desire (although I could be wrong as, as I said, re are not my strong point):
```import re

def is_hex(num):
hex_pattern = re.compile("[\-+]?[ ]*0[xX][0-9a-fA-F]+\$")
return bool(hex_pattern.match(num))

if __name__ == "__main__":
tests = ["0x0b045673",
"+0X0045673",
"0b01111010110",
"-0o467267",
"54637",
"-  0x0447863"]

for test in tests:
print(is_hex(test))
```
Results:
```>>>
True
True
False
False
False
True
>>>
```
which is what I wanted.

-Mek

Edit: Slight change as I found a mistake.

This post has been edited by Mekire: 13 June 2013 - 01:32 AM

#18 blindchicken11

Reputation: 0
• Posts: 93
• Joined: 19-September 12

Re: figuring out if a number is decimal, hexidecimal, or octal

Posted 13 June 2013 - 10:38 AM

Here's what I have right now:

```import string
import re

number = ""
infile = open(filename, "r")
for line in infile:
line.strip()
number += line
infile.close()
return number

def is_hex(number):

# hex_pattern = re.compile("[^+\-]?[ ]*0[xX](0|([1-9A-Fa-f][0-9A-Fa-f]))+\$")
hex_pattern = re.compile( r'^[+\-]?''0''[xX]''(0|''([1-9A-Fa-f][0-9A-Fa-f]*))\$')

return bool(hex_pattern.match(number))

def main():

filename = raw_input("Please enter the file name you wish to use. ")

print number

if is_hex(number) == "True":
print "You have entered a hexadecimal number."
else:
print "Nonsense."

main()

```

The .txt file I'm reading in contains -0x0 which should be recognized as a hexadecimal.
But it just prints out
"0x0" (this was just my test)
"Nonsense."

#19 jon.kiparsky

• Screw Trump (before he screws you)

Reputation: 10625
• Posts: 18,183
• Joined: 19-March 11

Re: figuring out if a number is decimal, hexidecimal, or octal

Posted 13 June 2013 - 10:58 AM

Good test, it localizes your problem nicely. So is_hex is returning false.

Those single-quotes look weird to me. What are they there for?

#20 blindchicken11

Reputation: 0
• Posts: 93
• Joined: 19-September 12

Re: figuring out if a number is decimal, hexidecimal, or octal

Posted 13 June 2013 - 11:01 AM

I thought that's how I had to set up the regex

#21 jon.kiparsky

• Screw Trump (before he screws you)

Reputation: 10625
• Posts: 18,183
• Joined: 19-March 11

Re: figuring out if a number is decimal, hexidecimal, or octal

Posted 13 June 2013 - 11:11 AM

Yeah, that's part of your problem. Get rid of the single-quotes. Also, it looks like you have an extra pair of parens. Try this:

```hex_pattern = re.compile( r'^[+\-]?0[xX](0|[1-9A-Fa-f][0-9A-Fa-f]*)\$')

```

#22 blindchicken11

Reputation: 0
• Posts: 93
• Joined: 19-September 12

Re: figuring out if a number is decimal, hexidecimal, or octal

Posted 13 June 2013 - 11:18 AM

Thank you! It's still printing out "Nonsense" for some reason. Am I stripping the file right? As in, getting rid of extra spaces and "\n"? I think I am, but maybe that's messing it up?

#23 jon.kiparsky

• Screw Trump (before he screws you)

Reputation: 10625
• Posts: 18,183
• Joined: 19-March 11

Re: figuring out if a number is decimal, hexidecimal, or octal

Posted 13 June 2013 - 11:28 AM

Possible. You can test that - run the script in the interpreter and examine the output from readFile.

python -i filename.py

is one way to do this. Then you can run readFile("myfile.txt") and see what you get.

#24 blindchicken11

Reputation: 0
• Posts: 93
• Joined: 19-September 12

Re: figuring out if a number is decimal, hexidecimal, or octal

Posted 13 June 2013 - 11:30 AM

The file is named number.txt The command you gave doesn't work, but I don't think I'm entering it right.

python -i number.py or number.txt doesn't work.

#25 jon.kiparsky

• Screw Trump (before he screws you)

Reputation: 10625
• Posts: 18,183
• Joined: 19-March 11

Re: figuring out if a number is decimal, hexidecimal, or octal

Posted 13 June 2013 - 11:34 AM

You'd use the name of your python source file, not the name of your data file

#26 blindchicken11

Reputation: 0
• Posts: 93
• Joined: 19-September 12

Re: figuring out if a number is decimal, hexidecimal, or octal

Posted 13 June 2013 - 11:44 AM

Ahh okay, thank you. It's showing '-0x0\n'

I'm almost positive the strip() is taking out that \n though.

#27 jon.kiparsky

• Screw Trump (before he screws you)

Reputation: 10625
• Posts: 18,183
• Joined: 19-March 11

Re: figuring out if a number is decimal, hexidecimal, or octal

Posted 13 June 2013 - 11:47 AM

Thought as much. You can use str.replace() to remove the '\n'

Or I guess you could remove the end anchor from your re pattern

blindchicken11, on 13 June 2013 - 01:44 PM, said:

Ahh okay, thank you. It's showing '-0x0\n'

I'm almost positive the strip() is taking out that \n though.

Sorry, brain fart on my part. strip will RETURN a copy of line with the \n removed. However, it will not change the value stored in line itself. You can use an assignment to do that:
```line = line.strip()

```

#28 blindchicken11

Reputation: 0
• Posts: 93
• Joined: 19-September 12

Re: figuring out if a number is decimal, hexidecimal, or octal

Posted 13 June 2013 - 11:50 AM

Thanks! Still returning "Nonsense"

#29 Mekire

Reputation: 118
• Posts: 216
• Joined: 11-January 13

Re: figuring out if a number is decimal, hexidecimal, or octal

Posted 13 June 2013 - 04:00 PM

blindchicken11, on 13 June 2013 - 06:50 PM, said:

Thanks! Still returning "Nonsense" />/>/>
As you are only getting a single number from the entire file (for now), you could try this:
```def readFile(filename):
with open(filename) as myfile:
```

-Mek

Edit: Oh and the bigger problem.

This:
```if is_hex(number) == "True":
```
should be
```if is_hex(number) == True:
```
or preferably
```if is_hex(number):
```

That is probably the real problem.

This post has been edited by Mekire: 13 June 2013 - 04:04 PM

#30 blindchicken11

Reputation: 0
• Posts: 93
• Joined: 19-September 12

Re: figuring out if a number is decimal, hexidecimal, or octal

Posted 18 June 2013 - 11:42 AM

Sorry this is late getting back, but that "True" was what did it! Had some minor issues after but figured them out. Thank you so much for your replies, and everyone else as well.