radix

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 17, 2021 License: MIT Imports: 5 Imported by: 0

README ยถ

Radix (Base) Conversion

Sequence of integers (byte, int) can be represent big number in positional numeral system with specified radix.

This project's functions convert input sequence from one radix to output sequence with other radix. Also encode/decode these sequences with specified alphabet.

Aim of this project to implement universal radix converter. But implementation of []byte input conversion using big.Int is more efficient than universtal implementation of []int input conversion. Check benchmarks into convert_test.go.

Base58 encoding (used by Bitcoin and others) builds on this conversion of big number from base 256 to base 58.

Examples
package main

import (
	"fmt"
	"log"
	
	"github.com/bergusman/radix-go"
)

func main() {
	out, err := radix.Convert([]int{1, 3, 3, 7}, 10, 16)
	if err != nil {
		log.Fatal(err)
	}

	str, err := radix.Encode(out, "0123456789ABCDEF")
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(str) // Output: 539

	out, err = radix.Convert([]int{1, 3, 3, 7}, 10, 5)
	if err != nil {
		log.Fatal(err)
	}

	str, err = radix.Encode(out, "๐ŸŒ‘๐ŸŒ˜๐ŸŒ—๐ŸŒ–๐ŸŒ•")
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(str) // Output: ๐ŸŒ—๐ŸŒ‘๐ŸŒ–๐ŸŒ—๐ŸŒ—
}
Base58
package main

import (
	"encoding/hex"
	"fmt"
	"log"

	"github.com/bergusman/radix-go"
)

func main() {
	addr, err := hex.DecodeString("8aee40b8e87eb05bc3b9ff902349bb2dd19a5e90")
	if err != nil {
		log.Fatal(err)
	}

	str, err := radix.Base58Encode(addr, radix.AlphabetBitcoin)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(str) // Output: 2wG9ewuHafzR4yG2hYu8U3YmpZxb
}

Can check here: http://lenschulwitz.com/base58

Documentation ยถ

Overview ยถ

Package radix implements radix conversions for integer sequences ([]byte or []int) and their encoding/deconding to string by specified alphabet.

Example ยถ
out, err := radix.Convert([]int{1, 3, 3, 7}, 10, 16)
if err != nil {
	log.Fatal(err)
}

str, err := radix.Encode(out, "0123456789ABCDEF")
if err != nil {
	log.Fatal(err)
}

fmt.Println(str)
Output:

539
Example (Bitcoin) ยถ
addr := []byte{138, 238, 64, 184, 232, 126, 176, 91, 195, 185, 255, 144, 35, 73, 187, 45, 209, 154, 94, 144}
str, err := radix.Base58Encode(addr, radix.AlphabetBitcoin)
if err != nil {
	log.Fatal(err)
}

fmt.Println(str)
Output:

2wG9ewuHafzR4yG2hYu8U3YmpZxb
Example (Emoji) ยถ
out, err := radix.Convert([]int{1, 3, 3, 7}, 10, 5)
if err != nil {
	log.Fatal(err)
}

str, err := radix.Encode(out, "๐ŸŒ‘๐ŸŒ˜๐ŸŒ—๐ŸŒ–๐ŸŒ•")
if err != nil {
	log.Fatal(err)
}

fmt.Println(str)
Output:

๐ŸŒ—๐ŸŒ‘๐ŸŒ–๐ŸŒ—๐ŸŒ—

Index ยถ

Examples ยถ

Constants ยถ

View Source
const AlphabetBitcoin = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
View Source
const AlphabetFlickr = "123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ"
View Source
const AlphabetRipple = "rpshnaf39wBUDNEGHJKLM4PQRST7VWXYZ2bcdeCg65jkm8oFqi1tuvAxyz"

Variables ยถ

View Source
var ErrBase58BadAlphabet = errors.New("base58: alphabet length less than 58")

Functions ยถ

func Base58Decode ยถ

func Base58Decode(input string, alphabet string) ([]byte, error)

func Base58Encode ยถ

func Base58Encode(input []byte, alphabet string) (string, error)

func Convert ยถ

func Convert(in []int, inrx int, outrx int) (out []int, err error)

Convert converts input (int) with radix (inrx) to ouput (out) with radix (outrx).

Example ยถ
out, err := radix.Convert([]int{1, 3, 3, 7}, 10, 2)
if err != nil {
	log.Fatal(err)
}

fmt.Println(out)
Output:

[1 0 1 0 0 1 1 1 0 0 1]

func ConvertBytes ยถ

func ConvertBytes(in []byte, outrx int) (out []byte, err error)

ConvertBytes converts input bytes (in) with 256 radix to output bytes (out) with radix (outrx).

func Decode ยถ

func Decode(input string, alphabet string) (output []int, err error)

Decode decodes input string by alphabet.

func DecodeBytes ยถ

func DecodeBytes(input string, alphabet string) (output []byte, err error)

DecodeBytes decodes input string by alphabet to bytes output.

func Encode ยถ

func Encode(input []int, alphabet string) (output string, err error)

Encode encodes input to string with alphabet.

Example ยถ
out, err := radix.Convert([]int{1, 3, 3, 7}, 10, 2)
if err != nil {
	log.Fatal(err)
}

str, err := radix.Encode(out, "01")
if err != nil {
	log.Fatal(err)
}

fmt.Println(str)
Output:

10100111001

func EncodeBytes ยถ

func EncodeBytes(input []byte, alphabet string) (output string, err error)

EncodeBytes encodes bytes input to string with alphabet.

Types ยถ

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL