baseconv

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Apr 14, 2021 License: MIT Imports: 2 Imported by: 12

README

About baseconv Build Status Coverage

baseconv is a simple Go package for converting between strings in arbitrary bases.

baseconv is useful when working with extremely large numbers stored in a string format and where there is a need to convert between different base representations (ie, decimal, hex, octal, etc). For example, baseconv is useful when converting from "url-safe" UUIDs to a decimal or binary representation.

baseconv was originally written to solve a similar use-case as PHP's base_convert function.

Installing

Install the package via the following:

$ go get -u github.com/kenshaw/baseconv

Using

baseconv can be used similarly to the following:

// _example/example.go
package main

import (
	"fmt"

	"github.com/kenshaw/baseconv"
)

func main() {
	valHex := "70b1d707eac2edf4c6389f440c7294b51fff57bb"
	fmt.Println("hex string: " + valHex)
	valDec, _ := baseconv.DecodeHexToDec(valHex)
	val62, _ := baseconv.Convert(valHex, baseconv.DigitsHex, baseconv.Digits62)
	val36, _ := baseconv.Convert(val62, baseconv.Digits62, baseconv.Digits36)

	fmt.Println("dec string: " + valDec)
	fmt.Println("62 string:  " + val62)
	fmt.Println("36 string:  " + val36)

	conVal36, _ := baseconv.Decode36ToDec(val36)
	fmt.Printf("dec and 36 values same: %t\n", valDec == conVal36)
}

Output:

$ cd $GOPATH/src/github.com/kenshaw/baseconv/example/ && go run example.go
hex string: 70b1d707eac2edf4c6389f440c7294b51fff57bb
dec string: 643372930067913326838082478477533553256088688571
62 string:  g4WuOGCMWgcPa70d91BezVvvvaX
36 string:  d5wjfaew7fypqn2ka6xpofdlwns9ha3
dec and 36 values same: true

Please see the GoDoc API page for a full API listing.

Documentation

Overview

Package baseconv converts a string in an arbitrary base to any other arbitrary base.

Index

Constants

View Source
const (
	// DigitsBin represents binary digits
	DigitsBin = "01"

	// DigitsOct represents octal Digits
	DigitsOct = "01234567"

	// DigitsDec represents decimal digits
	DigitsDec = "0123456789"

	// DigitsHex represents hex digits
	DigitsHex = "0123456789abcdef"

	// Digits36 represents base36 digits
	Digits36 = "0123456789abcdefghijklmnopqrstuvwxyz"

	// Digits62 represents base62 digits
	Digits62 = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"

	// Digits64 represents base64 digits
	Digits64 = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_"
)

Variables

This section is empty.

Functions

func Convert

func Convert(num, fromBase, toBase string) (string, error)

Convert num from specified base to a different base.

func Decode36ToDec

func Decode36ToDec(num string) (string, error)

Decode36ToDec decodes a string from Digits36 to DigitsDec.

func Decode62ToDec

func Decode62ToDec(num string) (string, error)

Decode62ToDec decodes a string from Digits62 to DigitsDec.

func Decode64ToDec

func Decode64ToDec(num string) (string, error)

Decode64ToDec decodes a string from Digits64 to DigitsDec.

func DecodeBinToDec

func DecodeBinToDec(num string) (string, error)

DecodeBinToDec decodes a string from DigitsBin to DigitsDec.

func DecodeHexToDec

func DecodeHexToDec(num string) (string, error)

DecodeHexToDec decodes a string from DigitsHex to DigitsDec.

func DecodeOctToDec

func DecodeOctToDec(num string) (string, error)

DecodeOctToDec decodes a string from DigitsOct to DigitsDec.

func Encode36FromDec

func Encode36FromDec(num string) (string, error)

Encode36FromDec encodes a string to Digits36 from DigitsDec.

func Encode62FromDec

func Encode62FromDec(num string) (string, error)

Encode62FromDec encodes a string to Digits62 to DigitsDec.

func Encode64FromDec

func Encode64FromDec(num string) (string, error)

Encode64FromDec encodes a string to Digits64 to DigitsDec.

func EncodeBinFromDec

func EncodeBinFromDec(num string) (string, error)

EncodeBinFromDec encodes a string to DigitsBin from DigitsDec.

func EncodeHexFromDec

func EncodeHexFromDec(num string) (string, error)

EncodeHexFromDec encodes a string to DigitsHex from DigitsDec.

func EncodeOctFromDec

func EncodeOctFromDec(num string) (string, error)

EncodeOctFromDec encodes a string to DigitsOct from DigitsDec.

Types

type Error

type Error string

Error is a base conversion error.

const (
	// ErrInvalidNumber is the invalid number error.
	ErrInvalidNumber Error = "invalid number"

	// ErrInvalidFromBase is the invalid from base error.
	ErrInvalidFromBase Error = "invalid fromBase"

	// ErrInvalidToBase is the invalid to base error.
	ErrInvalidToBase Error = "invalid toBase"
)

Error values.

func (Error) Error

func (err Error) Error() string

Error satisfies the error interface.

type InvalidCharacterError

type InvalidCharacterError struct {
	// contains filtered or unexported fields
}

InvalidCharacterError is an invalid character error.

func (*InvalidCharacterError) Error

func (err *InvalidCharacterError) Error() string

Error satisfies the error interface.

Directories

Path Synopsis
_example/example.go
_example/example.go

Jump to

Keyboard shortcuts

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