4

There are many inbulit functions like int(octal) which can be used to convert octal numbers into decimal numbers on command line but these doesn't work out in script . int(0671) returns 0671 in script, where as it represent decimal form of octal number on python command line. Help???

Thank You

3
  • 1
    Post some real code, with real results, it will make it easier for people to help. Commented Jun 15, 2010 at 2:37
  • Thanks actually I wanted to make my own encryption algorithm and decryption algorithm , encryption algorithm works fine and converts ascii value of the characters into alternate hexadecimal and octal representations. But when I tried decryption, problem occured following will help u to understand: NOTE: Following string is alternate octal and hexa of ascii value of char. ///////////////DECRYPTION/////// l="01630x7401620x6901560x67" f=len(l) k=0 d=0 x=[] for i in range(0,f,4): g=l[i:i+4] print g k=k+1 if(k%2==0): p=g print p else: p=int(g) print p Commented Jun 15, 2010 at 12:36
  • stackoverflow.com/questions/3045202/… Commented Jun 15, 2010 at 12:45

2 Answers 2

12

There's some confusion here -- pedantically (and with computers it's always best to be pedantic;-), there are no "octal numbers", there are strings which are octal representations of numbers (and other strings, more commonly encountered, which are their decimal representations, hexadecimal representations). The underlying numbers (integers) are a totally distinct type from any of the representations (by default their decimal representation is shown) -- e.g.:

>>> 2 + 2
4
>>> '2' + '2'
'22'

the quotes indicate strings (i.e., representations) -- and note that, per se, they have nothing to do with the numbers they may be representing.

So, one way to interpret your question is that you want to convert an octal representation into a decimal one (etc) -- that would be:

>>> str(int('0671', 8))
'441'

note the quoted (indicating strings, i.e., representations). int(s, 8) converts the string s into an integer as an octal representation (or raises an exception if the conversion can't work). str(n) produces the string form of number n (which, as I mentioned, is by default a decimal representation).

Sign up to request clarification or add additional context in comments.

1 Comment

2

First, the int() is useless. You can just type 0671.

Then, the number is stored in binary on the computer itself, you are only converting its string representation. The number itself doesn't change. Therefore, both of these will resolve to True, for example, which might've been the source of confusion:

0671 == 0671
0671 == 441

To ensure you will get the program to output the number in the base you want, the simplest way is to use string formatting, like so (if you want it to be in base 10):

print "%d" % 0671  # will output the number in base 10

4 Comments

Note that just typing 0671 no longer works in Python 3; try 0o671 instead (which works for all versions later than 2.6).
@MarkDickinson, 0o671 actually does work in Python 2.6. I tested with Python 2.6.3 on RHEL5 Linux.
@A-B-B: Yes, I didn't write that clearly. "all versions later than 2.6" was meant to include 2.6.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.