Write a program to convert base 36 to octal in python

How can I encode an integer with base 36 in Python and then decode it again?

Chris

42.8k16 gold badges137 silver badges152 bronze badges

asked Jul 25, 2009 at 11:32

1

Have you tried Wikipedia's sample code?

def base36encode(number, alphabet='0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'):
    """Converts an integer to a base36 string."""
    if not isinstance(number, (int, long)):
        raise TypeError('number must be an integer')

    base36 = ''
    sign = ''

    if number < 0:
        sign = '-'
        number = -number

    if 0 <= number < len(alphabet):
        return sign + alphabet[number]

    while number != 0:
        number, i = divmod(number, len(alphabet))
        base36 = alphabet[i] + base36

    return sign + base36

def base36decode(number):
    return int(number, 36)

print base36encode(1412823931503067241)
print base36decode('AQF8AA0006EH')

Write a program to convert base 36 to octal in python

Tim

40.4k18 gold badges126 silver badges142 bronze badges

answered Jul 25, 2009 at 11:35

kanngardkanngard

9377 silver badges9 bronze badges

7

I wish I had read this before. Here is the answer:

def base36encode(number):
    if not isinstance(number, (int, long)):
        raise TypeError('number must be an integer')
    is_negative = number < 0
    number = abs(number)

    alphabet, base36 = ['0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ', '']

    while number:
        number, i = divmod(number, 36)
        base36 = alphabet[i] + base36
    if is_negative:
        base36 = '-' + base36

    return base36 or alphabet[0]


def base36decode(number):
    return int(number, 36)

print(base36encode(1412823931503067241))
print(base36decode('AQF8AA0006EH'))
assert(base36decode(base36encode(-9223372036721928027)) == -9223372036721928027)

answered Jul 25, 2009 at 11:36

misteromistero

4,9698 gold badges28 silver badges27 bronze badges

3

from numpy import base_repr

num = base_repr(num, 36)
num = int(num, 36)

Here is information about numpy.base_repr.

answered Oct 21, 2015 at 23:15

1

You can use numpy's base_repr(...) for this.

import numpy as np

num = 2017

num = np.base_repr(num, 36)
print(num)  # 1K1

num = int(num, 36)
print(num)  # 2017

Here is some information about numpy, int(x, base=10), and np.base_repr(number, base=2, padding=0).

(This answer was originally submitted as an edit to @christopher-beland's answer, but was rejected in favor of its own answer.)

answered Feb 19, 2017 at 19:11

You could use https://github.com/tonyseek/python-base36.

$ pip install base36

and then

>>> import base36
>>> assert base36.dumps(19930503) == 'bv6h3'
>>> assert base36.loads('bv6h3') == 19930503

answered Jul 15, 2016 at 5:21

Jiangge ZhangJiangge Zhang

3,9514 gold badges24 silver badges33 bronze badges

4

terrible answer, but was just playing around with this an thought i'd share.

import string, math

int2base = lambda a, b: ''.join(
    [(string.digits +
      string.ascii_lowercase +
      string.ascii_uppercase)[(a // b ** i) % b]
     for i in range(int(math.log(a, b)), -1, -1)]
)

num = 1412823931503067241
test = int2base(num, 36)
test2 = int(test, 36)
print test2 == num

Write a program to convert base 36 to octal in python

akaihola

25.7k6 gold badges59 silver badges67 bronze badges

answered Jul 10, 2012 at 4:43

3

I benchmarked the example encoders provided in answers to this question. On my Ubuntu 18.10 laptop, Python 3.7, Jupyter, the %%timeit magic command, and the integer 4242424242424242 as the input, I got these results:

  • Wikipedia's sample code: 4.87 µs ± 300 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
  • @mistero's base36encode(): 3.62 µs ± 44.2 ns per loop
  • @user1036542's int2base: 10 µs ± 400 ns per loop (after fixing py37 compatibility)
  • @mbarkhau's int_to_base36(): 3.83 µs ± 28.8 ns per loop

All timings were mean ± std. dev. of 7 runs, 100000 loops each.

answered Apr 27, 2019 at 16:53

Write a program to convert base 36 to octal in python

akaiholaakaihola

25.7k6 gold badges59 silver badges67 bronze badges

1

If you are feeling functional

def b36_encode(i):
    if i < 0: return "-" + b36_encode(-i)
    if i < 36: return "0123456789abcdefghijklmnopqrstuvwxyz"[i]
    return b36_encode(i // 36) + b36_encode(i % 36)    

test

n = -919283471029384701938478
s = "-45p3wubacgd6s0fi"
assert int(s, base=36) == n
assert b36_encode(n) == s

answered Mar 2, 2020 at 23:24

Write a program to convert base 36 to octal in python

1

This works if you only care about positive integers.

def int_to_base36(num):
    """Converts a positive integer into a base36 string."""
    assert num >= 0
    digits = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'

    res = ''
    while not res or num > 0:
        num, i = divmod(num, 36)
        res = digits[i] + res
    return res

To convert back to int, just use int(num, 36). For a conversion of arbitrary bases see https://gist.github.com/mbarkhau/1b918cb3b4a2bdaf841c

answered Jul 31, 2015 at 13:02

mbarkhaumbarkhau

7,9404 gold badges29 silver badges34 bronze badges

What is the octal number of 36?

Problem Statements:.

How do you convert numbers to octal in Python?

To convert integer to octal format string in Python, call format(value, format) function and pass the integer for value parameter, and 'o' for format parameter. format() function returns a string with the octal representation of given integer.

How do you convert to octal base?

Solution: To convert decimal numbers to octal numbers, we need to divide 100 by the octal base number i.e. 8. Therefore, (100)10 ( 100 ) 10 = (144)8 ( 144 ) 8 .

What will be the correct syntax for the conversion of decimal to octal Python?

# Python program to convert decimal into other number systems dec = 344 print("The decimal value of", dec, "is:") print(bin(dec), "in binary.") print(oct(dec), "in octal.") print(hex(dec), "in hexadecimal.")