Skip to content
Class & function that converts a non-decimal number from any base to any other base. Works with bases 1 to 160.
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
BaseNumberConverter.swift
README.md

README.md

BaseNumberConverter

Made with Swift

Maintenance Ask Me Anything ! License Twitter Follow

Class & function that converts a non-decimal number from any base to any other base. Works with bases 1 to 160.

The following characters are used to encode numbers:

Base 1:
"|"

Bases 2...57, 59...63, 65...85:
0...9, a...z, A...Z, "!", "#", "$", "%", "&", "(", ")", "*", "+", "-", ";", "<", "=", ">", "?", "@", "^", "_", "'", "{", "|", "}", "~"

Base 58: 
1...9, a...k, m...z, A...H, J...N, P...Z (does not use 0, l, I, O)

Base 64: 
A...Z, a...z, 0...9, "+", "/"

Bases 86...160:
0...9, a...z, A...Z, ".", "-", ":", "+", "=", "^", "!", "/", "*", "?", "&", "<", ">", "(", ")", "[", "]", "{", "}", "@", "%", "$", "#", "α", "β", "γ", "δ", "ε", "ζ", "η", "θ", "ι", "κ", "λ", "μ", "ν", "ξ", "ο", "π", "ρ", "ς", "σ", "τ", "υ", "φ", "χ", "ψ", "ω", "グ", "ダ", "バ", "ム", "ヰ", "ァ", "ケ", "チ", "メ", "ヱ", "ヂ", "ヒ", "モ", "ヲ", "ィ", "コ", "ッ", "ビ", "ャ", "ン", "イ", "ゴ", "ツ", "ヤ", "ヴ", "ゥ", "サ", "ヅ", "フ", "ュ", "ヵ", "ウ", "ザ", "テ", "ブ", "ユ", "ヶ", "ェ", "シ", "デ", "ョ", "エ", "ト", "ヘ", "ヨ", "ォ", "ス", "ド", "ベ", "ラ", "オ"

Examples

BaseConverter.convert("0", fromBase: 2, toBase: 10)       //0
BaseConverter.convert("1", fromBase: 2, toBase: 10)       //1
BaseConverter.convert("10", fromBase: 2, toBase: 10)      //2
BaseConverter.convert("11", fromBase: 2, toBase: 10)      //3
BaseConverter.convert("100", fromBase: 2, toBase: 10)     //4

BaseConverter.convert("0", fromBase: 10, toBase: 2)       //0
BaseConverter.convert("1", fromBase: 10, toBase: 2)       //1
BaseConverter.convert("2", fromBase: 10, toBase: 2)       //10
BaseConverter.convert("11", fromBase: 10, toBase: 2)      //1011
BaseConverter.convert("12", fromBase: 10, toBase: 2)      //1100
BaseConverter.convert("13", fromBase: 10, toBase: 2)      //1101
BaseConverter.convert("-13", fromBase: 10, toBase: 2)     //-1101

BaseConverter.convert("0", fromBase: 10, toBase: 3)       //0
BaseConverter.convert("1", fromBase: 10, toBase: 3)       //1
BaseConverter.convert("2", fromBase: 10, toBase: 3)       //2
BaseConverter.convert("3", fromBase: 10, toBase: 3)       //10
BaseConverter.convert("-13", fromBase: 10, toBase: 3)     //-111

BaseConverter.convert("0", fromBase: 10, toBase: 36)      //0
BaseConverter.convert("1", fromBase: 10, toBase: 36)      //1
BaseConverter.convert("2", fromBase: 10, toBase: 36)      //2
BaseConverter.convert("3", fromBase: 10, toBase: 36)      //3
BaseConverter.convert("17", fromBase: 10, toBase: 36)     //H
BaseConverter.convert("22", fromBase: 10, toBase: 36)     //M
BaseConverter.convert("35", fromBase: 10, toBase: 36)     //Z
BaseConverter.convert("36", fromBase: 10, toBase: 36)     //10
BaseConverter.convert("37", fromBase: 10, toBase: 36)     //11
BaseConverter.convert("-37", fromBase: 10, toBase: 36)    //-11

BaseConverter.convert("-37", fromBase: 8, toBase: 4)      //-133
BaseConverter.convert("84", fromBase: 10, toBase: 85)     //~

BaseConverter.convert("61", fromBase: 10, toBase: 62)     //z
BaseConverter.convert("62", fromBase: 10, toBase: 62)     //10
BaseConverter.convert("2", fromBase: 10, toBase: 2)       //10
BaseConverter.convert("2", fromBase: 10, toBase: 1)       //||
BaseConverter.convert("4", fromBase: 10, toBase: 1)       //||||

BaseConverter.convert("||||", fromBase: 1, toBase: 10)    //4
BaseConverter.convert("1263", fromBase: 10, toBase: 85)   //E<
BaseConverter.convert("84", fromBase: 10, toBase: 85)     //~

BaseConverter.convert("159", fromBase: 10, toBase: 160)   //ラ
BaseConverter.convert("160", fromBase: 10, toBase: 160)   //10

BaseConverter.convert("1263", fromBase: 10, toBase: 161)  //nil (outside base range)
BaseConverter.convert("1263", fromBase: 0, toBase: 86)    //nil (outside base range)
BaseConverter.convert("", fromBase: 3, toBase: 86)        //nil (faulty input number)

Sources

https://www.calculand.com/unit-converter

You can’t perform that action at this time.