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

  • (2 Pages)
  • +
  • 1
  • 2

29 Replies - 2310 Views - Last Post: 18 June 2013 - 11:42 AM Rate Topic: -----

#16 blindchicken11  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 71
  • 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
Was This Post Helpful? 0
  • +
  • -

#17 Mekire  Icon User is offline

  • D.I.C Head

Reputation: 116
  • View blog
  • Posts: 212
  • 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

Was This Post Helpful? 0
  • +
  • -

#18 blindchicken11  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 71
  • 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
def readFile(filename):

    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. ")
    number = readFile(filename)

    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."
Was This Post Helpful? 0
  • +
  • -

#19 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7893
  • View blog
  • Posts: 13,424
  • 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?
Was This Post Helpful? 0
  • +
  • -

#20 blindchicken11  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 71
  • 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 :/
Was This Post Helpful? 0
  • +
  • -

#21 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7893
  • View blog
  • Posts: 13,424
  • 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]*)$')


Was This Post Helpful? 0
  • +
  • -

#22 blindchicken11  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 71
  • 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?
Was This Post Helpful? 0
  • +
  • -

#23 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7893
  • View blog
  • Posts: 13,424
  • 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.
Was This Post Helpful? 0
  • +
  • -

#24 blindchicken11  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 71
  • 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.
Was This Post Helpful? 0
  • +
  • -

#25 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7893
  • View blog
  • Posts: 13,424
  • 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
Was This Post Helpful? 0
  • +
  • -

#26 blindchicken11  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 71
  • 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.
Was This Post Helpful? 0
  • +
  • -

#27 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7893
  • View blog
  • Posts: 13,424
  • 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

View Postblindchicken11, 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()


Was This Post Helpful? 0
  • +
  • -

#28 blindchicken11  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 71
  • 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" :/
Was This Post Helpful? 0
  • +
  • -

#29 Mekire  Icon User is offline

  • D.I.C Head

Reputation: 116
  • View blog
  • Posts: 212
  • Joined: 11-January 13

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

Posted 13 June 2013 - 04:00 PM

View Postblindchicken11, 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:
        return myfile.read().strip()

-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

Was This Post Helpful? 0
  • +
  • -

#30 blindchicken11  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 71
  • 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.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2