Skip to content

math.bases

integers in hex, binary, octal or any base from 2 to 62, custom digits, spreadsheet column letters and phone keypad digits

Integers written in other bases: binary, octal, hex, or any base from 2 to 36.

Type a number in another base with a prefix: 0b1011 for binary, 0o17 for octal, 0xff for hex, or 36#zz for any base up to 36. A number remembers the base it was written in, so 0xff + 1 shows as 0x100.

Computers store negative numbers in two’s complement, where with a fixed number of bits -1 is all ones. Give that number of bits, as in -1 to hex(16), to see a negative number that way.

Tags: conversion

  • hex an integer in hex, the same as v to hex
  • bin an integer in binary, the same as v to bin
  • oct an integer in octal, the same as v to oct
  • dec an integer in decimal, the same as v to dec
  • base an integer in any base from 2 to 62, the same as v to base(b)
  • to_base an integer as text in a base from 2 to 62, or in your own digits: to_base(10, "ab") is “baba”
  • from_base the integer written in text in a base from 2 to 62, or in your own digits, the reverse of to_base
  • phone_digits the digits that a phone keypad gives letters, so “FLOWERS” is “3569377”
  • base_table a one-row table of an integer in binary, octal, decimal and hex
  • column_name the spreadsheet column letters for column n, counting from 1
  • column_number the column number, counting from 1, of spreadsheet column letters
  • digits the list of a number’s digits, in the base it was written in
  • from_digits put digits back together into a number, the reverse of digits
1
# to hex, to bin, to oct or to dec
2
255 to bin0b11111111
3
# to hex(bits): negatives in that many bits
4
-1 to hex(16)0xffff
5
# to base(b): any base from 2 to 62
6
255 to base(36)36#73
7
# to bases: binary, octal, decimal and hex side by side
8
255 to basestable([{bin: 0b11111111, oct: 0o377, dec: 255, hex: 0xff}])
Example outputCtrl+Enter to run
hex(v: int|float|str|bytes, bits?: int) -> int|str

an integer in hex, the same as v to hex

With bits, a negative number in two’s complement. A string is read as hex digits, with or without 0x, and bytes become the hex of each byte

1
hex(255)0xff
2
hex(-1, 16)0xffff
3
hex("0x65")0x65
4
hex(b"hi")"6869"
Example outputCtrl+Enter to run

See also bin, base, int, decode, encode

bin(v: int|float, bits?: int) -> int

an integer in binary, the same as v to bin

With bits, padded to that many bits and negatives in two’s complement

1
bin(10)0b1010
2
bin(5, 8)0b00000101
Example outputCtrl+Enter to run

See also hex, oct

oct(v: int|float, bits?: int) -> int

an integer in octal, the same as v to oct

1
oct(8)0o10
Example outputCtrl+Enter to run

See also hex, bin

dec(v: int|float, bits?: int) -> int

an integer in decimal, the same as v to dec

With bits, a negative number becomes the positive one with the same bits, so -1 in 8 bits is 255

1
dec(0xff)255
2
dec(-1, 8)255
Example outputCtrl+Enter to run

See also hex, int

base(v: int|float, b: int) -> int|str

an integer in any base from 2 to 62, the same as v to base(b)

Digits past 9 are lowercase letters, then capitals past base 36. Past base 36 the result is text, since only bases up to 36 are numbers

1
base(35, 36)36#z
2
base(10, 3)3#101
3
base(61, 62)"Z"
Example outputCtrl+Enter to run

See also hex, digits, to_base

to_base(n: int, digits: int|str) -> str

an integer as text in a base from 2 to 62, or in your own digits: to_base(10, "ab") is “baba”

A custom alphabet needs at least 2 distinct characters, and its length is the base

1
to_base(255, 62)"47"
2
to_base(10, "ab")"baba"
Example outputCtrl+Enter to run

See also from_base, base

from_base(s: str, digits: int|str) -> int

the integer written in text in a base from 2 to 62, or in your own digits, the reverse of to_base

1
from_base("zz", 36)1295
2
from_base("baba", "ab")10
Example outputCtrl+Enter to run

See also to_base

phone_digits(s: str) -> str

the digits that a phone keypad gives letters, so “FLOWERS” is “3569377”

Anything that isn’t a letter stays as it is

1
phone_digits("FLOWERS")"3569377"
2
phone_digits("1-800-GOT-JUNK")"1-800-468-5865"
Example outputCtrl+Enter to run

See also from_base

base_table(v: int|float) -> table

a one-row table of an integer in binary, octal, decimal and hex

1
255 to basestable([{bin: 0b11111111, oct: 0o377, dec: 255, hex: 0xff}])
2
base_table(10)table([{bin: 0b1010, oct: 0o12, dec: 10, hex: 0xa}])
Example outputCtrl+Enter to run

See also hex, bin, oct, dec

column_name(n: int) -> str

the spreadsheet column letters for column n, counting from 1

1
column_name(28)"AB"
2
column_name(703)"AAA"
Example outputCtrl+Enter to run

See also column_number

column_number(s: str) -> int

the column number, counting from 1, of spreadsheet column letters

1
column_number("AB")28
Example outputCtrl+Enter to run

See also column_name

digits(n: int) -> list

the list of a number’s digits, in the base it was written in

1
1234.digits[1, 2, 3, 4]
2
0b1011.digits[1, 0, 1, 1]
Example outputCtrl+Enter to run

See also from_digits

from_digits(xs: list, base: int = 10) -> int

put digits back together into a number, the reverse of digits

The result shows in the base the digits are in

1
[1, 2, 3].from_digits123
2
[1, 0, 1, 1].from_digits(2)0b1011
Example outputCtrl+Enter to run

See also digits

1
# 255 in binary
2
255 to bin0b11111111
3
# a base-36 number in decimal
4
36#zz to dec1295
5
# hex of a big int
6
factorial(20) to hex0x21c3677c82b40000
7
# results keep the base they were written in
8
0xff + 10x100
9
# -1 in 16-bit two's complement
10
-1 to hex(16)0xffff
11
# base 36
12
255 to base(36)36#73
13
# the sum of the digits of 2 to the 100th
14
(2 ** 100).digits.sum115
Example outputCtrl+Enter to run

Example results generated on 2026-11-04. Ones using now, today or randomness will differ when you run them.