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

• (2 Pages)
• 1
• 2

## 29 Replies - 4275 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=f498fd76a9baf74123675dce0c4977a6&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 blindchicken11

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

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

Posted 12 June 2013 - 06:27 PM

Hello. I'm trying to make a small program that reads in a file with only a single number in it, and then the program prints out whether it is a decimal, hexadecimal, or octal number and then displays the number itself. Here's the rules I made for what numbers are allowed:

An integer can be unambiguously specified in decimal, binary, octal or hexadecimal form. All of these forms can begin with an optional sign, which can be a "+" or a "-" with no separating characters between it and the rest of the integer.

A decimal integer can be a single zero or a sequence of one or more decimal digits that starts with a non-zero decimal digit. The decimal digits are 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9.

An octal number starts with a zero and is followed by a single zero or a non-empty sequence of octal digits that starts with a non-zero octal digit. An octal digit is one of 0, 1, 2, 3, 4, 5, 6 or 7.

A hexadecimal number starts with a zero and is followed by an upper or lower case "x", and either a single zero or a non-zero hexadecimal digit and a sequence of zero or more hexadecimal digits. Hexadecimal digits include the decimal digits and the letters a, b, c, d, e and f and their uppercase versions.

Notice how long and confusing this description has to be to ensure that it is unambiguous. "Leading zeros" aren't allowed in any of the representations, e.g., 0x007 is a bad hex number, and 007 is both a bad octal number and a bad decimal number.

I guess I'm just having trouble on how to get the program to figure out what kind of number is it.

Is This A Good Question/Topic? 0

## Replies To: figuring out if a number is decimal, hexidecimal, or octal

### #2 Mekire

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

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

Posted 12 June 2013 - 06:57 PM

As python supports binary, hex, and octal literals (in 2.6 and above), I would encourage you to use the same format. Then you wouldn't have to worry about signs or leading zeros). You could then check what base a number was in by checking if it contained "b","x", or "o" (be careful though as "b" is also valid in hex numbers).

-Mek

### #3 blindchicken11

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

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

Posted 12 June 2013 - 07:08 PM

Hmm, not exactly sure what you mean by literals?

Thanks for the reply by the way.

### #4 Mekire

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

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

Posted 12 June 2013 - 07:23 PM

x = 500
Is valid python syntax.

In python 2.6 and above, so are the following:
x = 0x0b045673
x = 0X0045673
x = 0b01111010110
x = -0o467267

Another method that you might find easier than my previous suggestion is just attempting to convert your strings to the three bases and catching the exception when you fail (this method however would have some disadvantages in that a number you may have intended to be decimal could still successfully convert to other bases).

As in:
try:
int(num,base)
except ValueError:
pass

-Mek

This post has been edited by Mekire: 12 June 2013 - 07:31 PM

### #5 jon.kiparsky

• Pancakes!

Reputation: 8825
• Posts: 15,234
• Joined: 19-March 11

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

Posted 12 June 2013 - 07:32 PM

blindchicken11, on 12 June 2013 - 08:27 PM, said:

Hello. I'm trying to make a small program that reads in a file with only a single number in it, and then the program prints out whether it is a decimal, hexadecimal, or octal number and then displays the number itself. Here's the rules I made for what numbers are allowed:

An integer can be unambiguously specified in decimal, binary, octal or hexadecimal form. All of these forms can begin with an optional sign, which can be a "+" or a "-" with no separating characters between it and the rest of the integer.

A decimal integer can be a single zero or a sequence of one or more decimal digits that starts with a non-zero decimal digit. The decimal digits are 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9.

An octal number starts with a zero and is followed by a single zero or a non-empty sequence of octal digits that starts with a non-zero octal digit. An octal digit is one of 0, 1, 2, 3, 4, 5, 6 or 7.

A hexadecimal number starts with a zero and is followed by an upper or lower case "x", and either a single zero or a non-zero hexadecimal digit and a sequence of zero or more hexadecimal digits. Hexadecimal digits include the decimal digits and the letters a, b, c, d, e and f and their uppercase versions.

Notice how long and confusing this description has to be to ensure that it is unambiguous. "Leading zeros" aren't allowed in any of the representations, e.g., 0x007 is a bad hex number, and 007 is both a bad octal number and a bad decimal number.

I guess I'm just having trouble on how to get the program to figure out what kind of number is it.

These rules are not quite correct, though - by your rules, 7365420 can be either decimal or octal or hexadecimal.

In java, initial zero indicates octal and 0x indicates hex, and 0b indicates binary (this may be new in 7, I haven't really made use of this before). These are reasonable standards, you should consider using them.

### #6 blindchicken11

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

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

Posted 12 June 2013 - 07:36 PM

Sorry I'm kind of new to Python. Right now I have:

number = []
infile = open(filename, "r")
for line in infile:
line.strip("\n")
for c in line:
number.append(c)
infile.close()
return number

def main():

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

if (number[0] == 0) and (number[1] == "x" or "X"):
print("The number %s is a hexadecimal number." %s)

main()

The only thing that gets printed out is an "x".

### #7 blindchicken11

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

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

Posted 12 June 2013 - 07:43 PM

For my rules, a decimal has to be a single zero OR a sequence starting off with a non-zero number.
An octal starts with a zero and is followed by a single zero or any number of non-zero octal digits.

So the number 7365420 would have to be a decimal since it doesn't start off with a zero.
And the hexadecimal ones have to start off with a zero and an x or X.

### #8 jon.kiparsky

• Pancakes!

Reputation: 8825
• Posts: 15,234
• Joined: 19-March 11

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

Posted 12 June 2013 - 07:53 PM

Oh, okay. I was confused by your description. Carry on, then.
if (number[0] == 0) and (number[1] == "x" or "X"):

This is not doing what you think it is.
A and B

is true iff A is true, and B is also true.
C or D

is true iff C is true, or D is true.

So,

(A) and (C or D)

is always true if either C or D is always true.

And "X" is always true.

if "X":
print "foo"
else:
print "bar"

if (number[0] == 0) and (number[1] == "x" or "X"):

There is no way that number [0] will ever equal 0, since you're reading a string. It might equal "0", but that's a totally different thing.

And what is s here?
print("The number %s is a hexadecimal number." %s)

You know, looking over this, I can see how it doesn't do what you want, but I don't see how it produces an "X". Are you sure that's what happens when you run this code?

This post has been edited by jon.kiparsky: 12 June 2013 - 07:57 PM

### #9 blindchicken11

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

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

Posted 12 June 2013 - 08:01 PM

Yep! But I think I found what I'm looking for, just not sure how to do it. I want to use regular expressions for this, something like

re.match(r'[+\-]?'                      # optional sign
'[xX]'                        # upper or lower case "x"
'0|'                          # single zero or
'([1-9A-Fa-f][0-9A-Fa-f]*)',  # a non-zero hexadecimal digit and a
# sequence of zero or more hexadecimal digits
text)

But I've never used them before, and I'm not exactly sure how to put that into my code.

This post has been edited by blindchicken11: 12 June 2013 - 08:05 PM

### #10 jon.kiparsky

• Pancakes!

Reputation: 8825
• Posts: 15,234
• Joined: 19-March 11

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

Posted 12 June 2013 - 08:38 PM

You should probably start here

### #11 Mekire

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

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

Posted 12 June 2013 - 08:45 PM

I really think regular expressions are quite unnecessary for this project.
You can create a simple dictionary and use the in operator.
Seems you are over-complicating the problem.

-Mek

### #12 blindchicken11

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

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

Posted 12 June 2013 - 09:38 PM

You're probably right. But the whole regex things is what I had in mind. They seem interesting, I would love to able to learn them. I'm not sure how I would incorporate the number in the file into the regex part

### #13 Mekire

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

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

Posted 12 June 2013 - 09:44 PM

Don't get me wrong. You should definitely learn regular expressions. I'm personally not very good at them myself. It is just very common to see people trying to solve problems with regex that are more simply done with builtins. It is simply a matter of selecting the appropriate tool for the job.

-Mek

### #14 blindchicken11

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

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

Posted 12 June 2013 - 10:45 PM

Do you know how to use the match function with regex by chance? I don't know where to put the string that I'm trying to match it with in the regex code. I'm looking at examples online but it seems different.

### #15 Mekire

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

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

Posted 12 June 2013 - 10:57 PM

>>> help(re.match)
Help on function match in module re:

match(pattern, string, flags=0)
Try to apply the pattern at the start of the string, returning
a match object, or None if no match was found.

So for an (awful) example:
>>> bool(re.match("r*eally","rrrrrrrreally"))
True

Note that match checks from the beginning of the string, so you might actually want re.search instead. Not sure.

-Mek