package faiNumber
import "github.com/kevinhng86/faiNumber-Go/faiNumber"
Package faiNumber mainly deals with converting numerical strings to a value of an int data type, converting a value of an int data type to a numerical string. faiNumber is an extremely fast library for parsing string and converting int values to strings. Be it whether if it is an overflow, underflow, empty strings where it shouldn't be, or an invalid input, faiNumber functions were designed to always return an error with a code when an error has occurred where it is possible to have an error.
faiNumber is extremely fast. faiNumber may be the fastest golang library for numerical string parsing and converting an int data type to a numerical string. faiNumber functions were tested and is much faster than strconv package. But faiNumber only support parsing decimal, binary, octal, hexadecimal string to int32, uint32, int64 or uint64 data types. For converting an int value to a numerical string, faiNumber only support convert int32, uint32, int64 or uint64 to a string of decimal, binary, octal or hexadecimal.
Each of faiNumber parsing function were written separately for each usage case and they were optimize for speed. For converting to and from each data type of int32, uint32, int64, uint64, a separate function was created to optimize for speed and ensure no type casting is required for using any of faiNumber function.
Benchmark
faiNumber's benchmark compare to strconv can be found here: https://github.com/kevinhng86/faiNumber-Go/blob/main/benchmark.md
Parsing numerical strings to int data type
For converting string to int faiNumber has the following functions: DecToInt32, DecToUInt32, DecToInt64, DecToUInt64, BinaryToInt32, BinaryToInt32AsUnsigned, BinaryToUInt32, BinaryToInt64, BinaryToInt64AsUnsigned, BinaryToUInt64, OctalToInt32, OctalToUInt32, OctalToInt64, OctalToUInt64, HexToInt32, HexToUInt32, HexToInt64, and HexToUInt64.
i, err := faiNumber.DecToInt32("-99")
i, err := faiNumber.DecToUInt32("99")
i, err := faiNumber.DecToInt64("-102")
i, err := faiNumber.DecToUInt64("102")
i, err := faiNumber.BinaryToInt32("-101")
i, err := faiNumber.BinaryToInt32AsUnsigned("10101010")
i, err := faiNumber.BinaryToUInt32("1010")
i, err := faiNumber.BinaryToInt64("-11110000")
i, err := faiNumber.BinaryToInt64AsUnsigned("11110000")
i, err := faiNumber.BinaryToUInt64("11110000")
i, err := faiNumber.OctalToInt32("-17")
i, err := faiNumber.OctalToUInt32("17")
i, err := faiNumber.OctalToInt64("-3000")
i, err := faiNumber.OctalToUInt64("3000")
i, err := faiNumber.HexToInt32("-FF")
i, err := faiNumber.HexToUInt32("FF")
i, err := faiNumber.HexToInt64("-AB")
i, err := faiNumber.HexToUInt64("AB")
Converting an int value to string
For converting an int value to string faiNumber has the following functions: Int32ToDec, UInt32ToDec, Int32ToBinary, UInt32ToBinary, Int32ToBinaryAsUnsigned, Int32ToOctal, UInt32ToOctal, Int32ToHex, UInt32ToHex, Int64ToDec, UInt64ToDec, Int64ToBinary, UInt64ToBinary, Int64ToBinaryAsUnsigned, Int64ToOctal, UInt64ToOctal, Int64ToHex, and UInt64ToHex.
Index
- Variables
- func BinaryToInt32(str string) (int32, error)
- func BinaryToInt32AsUnsigned(str string) (int32, error)
- func BinaryToInt64(str string) (int64, error)
- func BinaryToInt64AsUnsigned(str string) (int64, error)
- func BinaryToUInt32(str string) (uint32, error)
- func BinaryToUInt64(str string) (uint64, error)
- func DecToInt32(str string) (int32, error)
- func DecToInt64(str string) (int64, error)
- func DecToUInt32(str string) (uint32, error)
- func DecToUInt64(str string) (uint64, error)
- func HexToInt32(str string) (int32, error)
- func HexToInt64(str string) (int64, error)
- func HexToUInt32(str string) (uint32, error)
- func HexToUInt64(str string) (uint64, error)
- func Int32ToBinary(number int32) (str string)
- func Int32ToBinaryAsUnsigned(number int32) (str string)
- func Int32ToDec(number int32) (str string)
- func Int32ToHex(number int32) (str string)
- func Int32ToOctal(number int32) (str string)
- func Int64ToBinary(number int64) (str string)
- func Int64ToBinaryAsUnsigned(number int64) (str string)
- func Int64ToDec(number int64) (str string)
- func Int64ToHex(number int64) (str string)
- func Int64ToOctal(number int64) (str string)
- func MakeErrorEmptyString(FunctionName string, input string) error
- func MakeErrorInvalidFormat(FunctionName string, input string) error
- func MakeErrorOverflow(FunctionName string, input string) error
- func MakeErrorUnderflow(FunctionName string, input string) error
- func OctalToInt32(str string) (int32, error)
- func OctalToInt64(str string) (int64, error)
- func OctalToUInt32(str string) (uint32, error)
- func OctalToUInt64(str string) (uint64, error)
- func UInt32ToBinary(number uint32) (str string)
- func UInt32ToDec(number uint32) (str string)
- func UInt32ToHex(number uint32) (str string)
- func UInt32ToOctal(number uint32) (str string)
- func UInt64ToBinary(number uint64) (str string)
- func UInt64ToDec(number uint64) (str string)
- func UInt64ToHex(number uint64) (str string)
- func UInt64ToOctal(number uint64) (str string)
- type FNError
Examples
- BinaryToInt32
- BinaryToInt32AsUnsigned
- BinaryToInt64
- BinaryToInt64AsUnsigned
- BinaryToUInt32
- BinaryToUInt64
- DecToInt32
- DecToInt64
- DecToUInt32
- DecToUInt64
- FNError
- HexToInt32
- HexToInt64
- HexToUInt32
- HexToUInt64
- Int32ToBinary
- Int32ToBinaryAsUnsigned
- Int32ToDec
- Int32ToHex
- Int32ToOctal
- Int64ToBinary
- Int64ToBinaryAsUnsigned
- Int64ToDec
- Int64ToHex
- Int64ToOctal
- OctalToInt32
- OctalToInt64
- OctalToUInt32
- OctalToUInt64
- UInt32ToBinary
- UInt32ToDec
- UInt32ToHex
- UInt32ToOctal
- UInt64ToBinary
- UInt64ToDec
- UInt64ToHex
- UInt64ToOctal
Variables
var Version [3]byte = [3]byte{1, 0, 0}
faiNumber version number, which can't be declare as a constant. Version is Major.Minor.Security. Major release version number is at index 0. Minor release version number is at index 1. Security fix release version is at index 2.
Functions
func BinaryToInt32
func BinaryToInt32(str string) (int32, error)
BinaryToInt32 parse the input string as a signed binary integer string to an int32 value. The input string can begin with a leading sign: "+" or "-" to determine whether the input string represent a positive or negative value.
The errors that BinaryToInt32 returns are type *FNError and include FNError.Input = str, FNError.FromFunction = "faiNumber.BinaryToInt32", FNError.Message = Error message, and FNError.Code = The error code. The error code and error message is different depend on which type of error.
If str is empty then Error Code 1 is return. faiNumber classified this as an Empty String Error.
If str contains invalid binary digits then Error Code 2 is return. faiNumber classfied this as an Invalid Format Error.
If the value corresponding to str is smaller than the minimum value an int32 can hold then Error Code 3 is return. faiNumber classified this and an Underflow Error.
If the value corresponding to str is larger than the maximum value an int32 can hold then Error Code 4 is return.
faiNumber classfied this as an Overflow Error.
Binary To Int Example Functions Start
Output:Example
package main
import (
"fmt"
"github.com/kevinhng86/faiNumber-Go/faiNumber"
)
func main() {
str := "-10101010"
output, err := faiNumber.BinaryToInt32(str)
if err != nil {
fmt.Println("Error handling")
}
fmt.Printf("%T, %b\n", output, output)
}
int32, -10101010
func BinaryToInt32AsUnsigned
func BinaryToInt32AsUnsigned(str string) (int32, error)
BinaryToInt32AsUnsigned parse the input string as a binary integer string to an int32 value. This function treat the int32 data type as unsigned and parse the raw bit value of the input string without considering negative values. Sign parsing is not permitted. If there is a sign prefix then the function will yield an Invalid Format Error.
The errors that BinaryToInt32AsUnsigned returns are type *FNError and include FNError.Input = str, FNError.FromFunction = "faiNumber.BinaryToInt32AsUnsigned", FNError.Message = Error message, and FNError.Code = The error code. The error code and error message is different depend on which type of error.
If str is empty then Error Code 1 is return. faiNumber classified this as an Empty String Error.
If str contains invalid binary digits then Error Code 2 is return. faiNumber classfied this as an Invalid Format Error.
If the parsing length of str, excluding leading zeroes is larger than 32 then Error Code 4 is return.
faiNumber classfied this as an Overflow Error.
Output:Example
package main
import (
"fmt"
"github.com/kevinhng86/faiNumber-Go/faiNumber"
)
func main() {
str := "11111111111111111111111111111111"
output, err := faiNumber.BinaryToInt32AsUnsigned(str)
if err != nil {
fmt.Println("Error handling")
}
fmt.Printf("%T, %v\n", output, output)
}
int32, -1
func BinaryToInt64
func BinaryToInt64(str string) (int64, error)
BinaryToInt64 parse the input string as a signed binary integer string to an int64 value. The input string can begin with a leading sign: "+" or "-" to determine whether the input string represent a positive or negative value.
The errors that BinaryToInt64 returns are type *FNError and include FNError.Input = str, FNError.FromFunction = "faiNumber.BinaryToInt64", FNError.Message = Error message, and FNError.Code = The error code. The error code and error message is different depend on which type of error.
If str is empty then Error Code 1 is return. faiNumber classified this as an Empty String Error.
If str contains invalid binary digits then Error Code 2 is return. faiNumber classfied this as an Invalid Format Error.
If the value corresponding to str is smaller than the minimum value an int64 can hold then Error Code 3 is return. faiNumber classified this and an Underflow Error.
If the value corresponding to str is larger than the maximum value an int64 can hold then Error Code 4 is return.
faiNumber classfied this as an Overflow Error.
Output:Example
package main
import (
"fmt"
"github.com/kevinhng86/faiNumber-Go/faiNumber"
)
func main() {
str := "-11111001101100001111110011011110101"
output, err := faiNumber.BinaryToInt64(str)
if err != nil {
fmt.Println("Error handling")
}
fmt.Printf("%T, %b\n", output, output)
}
int64, -11111001101100001111110011011110101
func BinaryToInt64AsUnsigned
func BinaryToInt64AsUnsigned(str string) (int64, error)
BinaryToInt64AsUnsigned parse the input string as a binary integer string to an int64 value. This function treat the int64 data type as unsigned and parse the raw bit value of the input string without considering negative values. Sign parsing is not permitted. If there is a sign prefix then the function will yield an Invalid Format Error.
The errors that BinaryToInt64AsUnsigned returns are type *FNError and include FNError.Input = str, FNError.FromFunction = "faiNumber.BinaryToInt64AsUnsigned", FNError.Message = Error message, and FNError.Code = The error code. The error code and error message is different depend on which type of error.
If str is empty then Error Code 1 is return. faiNumber classified this as an Empty String Error.
If str contains invalid binary digits then Error Code 2 is return. faiNumber classfied this as an Invalid Format Error.
If the parsing length of str, excluding leading zeroes is larger than 64 then Error Code 4 is return.
faiNumber classfied this as an Overflow Error.
Output:Example
package main
import (
"fmt"
"github.com/kevinhng86/faiNumber-Go/faiNumber"
)
func main() {
str := "1111111111111111111111111111111111111111111111111111111111111111"
output, err := faiNumber.BinaryToInt64AsUnsigned(str)
if err != nil {
fmt.Println("Error handling")
}
fmt.Printf("%T, %v\n", output, output)
}
int64, -1
func BinaryToUInt32
func BinaryToUInt32(str string) (uint32, error)
BinaryToUInt32 parse the input string as an unsigned binary integer string to an uint32 value. Sign parsing is not permitted. If there is a sign prefix then the function will yield an Invalid Format Error.
The errors that BinaryToUInt32 returns are type *FNError and include FNError.Input = str, FNError.FromFunction = "faiNumber.BinaryToUInt32", FNError.Message = Error message, and FNError.Code = The error code. The error code and error message is different depend on which type of error.
If str is empty then Error Code 1 is return. faiNumber classified this as an Empty String Error.
If str contains invalid binary digits then Error Code 2 is return. faiNumber classfied this as an Invalid Format Error.
If the value corresponding to str is larger than the maximum value an uint32 can hold then Error Code 4 is return.
faiNumber classfied this as an Overflow Error.
Output:Example
package main
import (
"fmt"
"github.com/kevinhng86/faiNumber-Go/faiNumber"
)
func main() {
str := "1111100000"
output, err := faiNumber.BinaryToUInt32(str)
if err != nil {
fmt.Println("Error handling")
}
fmt.Printf("%T, %b\n", output, output)
}
uint32, 1111100000
func BinaryToUInt64
func BinaryToUInt64(str string) (uint64, error)
BinaryToUInt64 parse the input string as an unsigned binary integer string to an uint64 value. Sign parsing is not permitted. If there is a sign prefix then the function will yield an Invalid Format Error.
The errors that BinaryToUInt64 returns are type *FNError and include FNError.Input = str, FNError.FromFunction = "faiNumber.BinaryToUInt64", FNError.Message = Error message, and FNError.Code = The error code. The error code and error message is different depend on which type of error.
If str is empty then Error Code 1 is return. faiNumber classified this as an Empty String Error.
If str contains invalid binary digits then Error Code 2 is return. faiNumber classfied this as an Invalid Format Error.
If the value corresponding to str is larger than the maximum value an uint64 can hold then Error Code 4 is return.
faiNumber classfied this as an Overflow Error.
Output:Example
package main
import (
"fmt"
"github.com/kevinhng86/faiNumber-Go/faiNumber"
)
func main() {
str := "1110110110110101011001010100101010110010101"
output, err := faiNumber.BinaryToUInt64(str)
if err != nil {
fmt.Println("Error handling")
}
fmt.Printf("%T, %b\n", output, output)
}
uint64, 1110110110110101011001010100101010110010101
func DecToInt32
func DecToInt32(str string) (int32, error)
DecToInt32 parse the input string as a signed decimal integer string to an int32 value. The input string can begin with a leading sign: "+" or "-" to determine whether the input string represent a positive or negative value.
The errors that DecToInt32 returns are type *FNError and include FNError.Input = str, FNError.FromFunction = "faiNumber.DecToInt32", FNError.Message = Error message, and FNError.Code = The error code. The error code and error message is different depend on which type of error.
If str is empty then Error Code 1 is return. faiNumber classified this as an Empty String Error.
If str contains invalid decimal digits then Error Code 2 is return. faiNumber classfied this as an Invalid Format Error.
If the value corresponding to str is smaller than the minimum value an int32 can hold then Error Code 3 is return. faiNumber classified this and an Underflow Error.
If the value corresponding to str is larger than the maximum value an int32 can hold then Error Code 4 is return.
faiNumber classfied this as an Overflow Error.
Decimal To Int Example Functions Start
Output:Example
package main
import (
"fmt"
"github.com/kevinhng86/faiNumber-Go/faiNumber"
)
func main() {
str := "-5487458"
output, err := faiNumber.DecToInt32(str)
if err != nil {
fmt.Println("Error handling")
}
fmt.Printf("%T, %v\n", output, output)
}
int32, -5487458
func DecToInt64
func DecToInt64(str string) (int64, error)
DecToInt64 parse the input string as a signed decimal integer string to an int64 value. The input string can begin with a leading sign: "+" or "-" to determine whether the input string represent a positive or negative value.
The errors that DecToInt64 returns are type *FNError and include FNError.Input = str, FNError.FromFunction = "faiNumber.DecToInt64", FNError.Message = Error message, and FNError.Code = The error code. The error code and error message is different depend on which type of error.
If str is empty then Error Code 1 is return. faiNumber classified this as an Empty String Error.
If str contains invalid decimal digits then Error Code 2 is return. faiNumber classfied this as an Invalid Format Error.
If the value corresponding to str is smaller than the minimum value an int64 can hold then Error Code 3 is return. faiNumber classified this and an Underflow Error.
If the value corresponding to str is larger than the maximum value an int64 can hold then Error Code 4 is return.
faiNumber classfied this as an Overflow Error.
Output:Example
package main
import (
"fmt"
"github.com/kevinhng86/faiNumber-Go/faiNumber"
)
func main() {
str := "-987456321487564"
output, err := faiNumber.DecToInt64(str)
if err != nil {
fmt.Println("Error handling")
}
fmt.Printf("%T, %v\n", output, output)
}
int64, -987456321487564
func DecToUInt32
func DecToUInt32(str string) (uint32, error)
DecToUInt32 parse the input string as an unsigned decimal integer string to an uint32 value. Sign parsing is not permitted. If there is a sign prefix then the function will yield an Invalid Format Error.
The errors that DecToUInt32 returns are type *FNError and include FNError.Input = str, FNError.FromFunction = "faiNumber.DecToUInt32", FNError.Message = Error message, and FNError.Code = The error code. The error code and error message is different depend on which type of error.
If str is empty then Error Code 1 is return. faiNumber classified this as an Empty String Error.
If str contains invalid decimal digits then Error Code 2 is return. faiNumber classfied this as an Invalid Format Error.
If the value corresponding to str is larger than the maximum value an uint32 can hold then Error Code 4 is return.
faiNumber classfied this as an Overflow Error.
Output:Example
package main
import (
"fmt"
"github.com/kevinhng86/faiNumber-Go/faiNumber"
)
func main() {
str := "987"
output, err := faiNumber.DecToUInt32(str)
if err != nil {
fmt.Println("Error handling")
}
fmt.Printf("%T, %v\n", output, output)
}
uint32, 987
func DecToUInt64
func DecToUInt64(str string) (uint64, error)
DecToUInt64 parse the input string as an unsigned decimal integer string to an uint64 value. Sign parsing is not permitted. If there is a sign prefix then the function will yield an Invalid Format Error.
The errors that DecToUInt64 returns are type *FNError and include FNError.Input = str, FNError.FromFunction = "faiNumber.DecToUInt64", FNError.Message = Error message, and FNError.Code = The error code. The error code and error message is different depend on which type of error.
If str is empty then Error Code 1 is return. faiNumber classified this as an Empty String Error.
If str contains invalid decimal digits then Error Code 2 is return. faiNumber classfied this as an Invalid Format Error.
If the value corresponding to str is larger than the maximum value an uint64 can hold then Error Code 4 is return.
faiNumber classfied this as an Overflow Error.
Output:Example
package main
import (
"fmt"
"github.com/kevinhng86/faiNumber-Go/faiNumber"
)
func main() {
str := "98745641236549"
output, err := faiNumber.DecToUInt64(str)
if err != nil {
fmt.Println("Error handling")
}
fmt.Printf("%T, %v\n", output, output)
}
uint64, 98745641236549
func HexToInt32
func HexToInt32(str string) (int32, error)
HexToInt32 parse the input string as a signed hexadecimal integer string to an int32 value. The input string can begin with a leading sign: "+" or "-" to determine whether the input string represent a positive or negative value.
The errors that HexToInt32 returns are type *FNError and include FNError.Input = str, FNError.FromFunction = "faiNumber.HexToInt32", FNError.Message = Error message, and FNError.Code = The error code. The error code and error message is different depend on which type of error.
If str is empty then Error Code 1 is return. faiNumber classified this as an Empty String Error.
If str contains invalid hexadecimal digits then Error Code 2 is return. faiNumber classfied this as an Invalid Format Error.
If the value corresponding to str is smaller than the minimum value an int32 can hold then Error Code 3 is return. faiNumber classified this and an Underflow Error.
If the value corresponding to str is larger than the maximum value an int32 can hold then Error Code 4 is return.
faiNumber classfied this as an Overflow Error.
Hex To Int Example Functions Start
Output:Example
package main
import (
"fmt"
"github.com/kevinhng86/faiNumber-Go/faiNumber"
)
func main() {
str := "-F10A"
output, err := faiNumber.HexToInt32(str)
if err != nil {
fmt.Println("Error handling")
}
fmt.Printf("%T, %X\n", output, output)
}
int32, -F10A
func HexToInt64
func HexToInt64(str string) (int64, error)
HexToInt64 parse the input string as a signed hexadecimal integer string to an int64 value. The input string can begin with a leading sign: "+" or "-" to determine whether the input string represent a positive or negative value.
The errors that HexToInt64 returns are type *FNError and include FNError.Input = str, FNError.FromFunction = "faiNumber.HexToInt64", FNError.Message = Error message, and FNError.Code = The error code. The error code and error message is different depend on which type of error.
If str is empty then Error Code 1 is return. faiNumber classified this as an Empty String Error.
If str contains invalid hexadecimal digits then Error Code 2 is return. faiNumber classfied this as an Invalid Format Error.
If the value corresponding to str is smaller than the minimum value an int64 can hold then Error Code 3 is return. faiNumber classified this and an Underflow Error.
If the value corresponding to str is larger than the maximum value an int64 can hold then Error Code 4 is return.
faiNumber classfied this as an Overflow Error.
Output:Example
package main
import (
"fmt"
"github.com/kevinhng86/faiNumber-Go/faiNumber"
)
func main() {
str := "-8ABCDEFABC"
output, err := faiNumber.HexToInt64(str)
if err != nil {
fmt.Println("Error handling")
}
fmt.Printf("%T, %X\n", output, output)
}
int64, -8ABCDEFABC
func HexToUInt32
func HexToUInt32(str string) (uint32, error)
HexToUInt32 parse the input string as an unsigned hexadecimal integer string to an uint32 value. Sign parsing is not permitted. If there is a sign prefix then the function will yield an Invalid Format Error.
The errors that HexToUInt32 returns are type *FNError and include FNError.Input = str, FNError.FromFunction = "faiNumber.HexToUInt32", FNError.Message = Error message, and FNError.Code = The error code. The error code and error message is different depend on which type of error.
If str is empty then Error Code 1 is return. faiNumber classified this as an Empty String Error.
If str contains invalid hexadecimal digits then Error Code 2 is return. faiNumber classfied this as an Invalid Format Error.
If the value corresponding to str is larger than the maximum value an uint32 can hold then Error Code 4 is return.
faiNumber classfied this as an Overflow Error.
Output:Example
package main
import (
"fmt"
"github.com/kevinhng86/faiNumber-Go/faiNumber"
)
func main() {
str := "FFFFAAAA"
output, err := faiNumber.HexToUInt32(str)
if err != nil {
fmt.Println("Error handling")
}
fmt.Printf("%T, %X\n", output, output)
}
uint32, FFFFAAAA
func HexToUInt64
func HexToUInt64(str string) (uint64, error)
HexToUInt64 parse the input string as an unsigned hexadecimal integer string to an uint64 value. Sign parsing is not permitted. If there is a sign prefix then the function will yield an Invalid Format Error.
The errors that HexToUInt64 returns are type *FNError and include FNError.Input = str, FNError.FromFunction = "faiNumber.HexToUInt64", FNError.Message = Error message, and FNError.Code = The error code. The error code and error message is different depend on which type of error.
If str is empty then Error Code 1 is return. faiNumber classified this as an Empty String Error.
If str contains invalid hexadecimal digits then Error Code 2 is return. faiNumber classfied this as an Invalid Format Error.
If the value corresponding to str is larger than the maximum value an uint64 can hold then Error Code 4 is return.
faiNumber classfied this as an Overflow Error.
Output:Example
package main
import (
"fmt"
"github.com/kevinhng86/faiNumber-Go/faiNumber"
)
func main() {
str := "FABC1234FABC"
output, err := faiNumber.HexToUInt64(str)
if err != nil {
fmt.Println("Error handling")
}
fmt.Printf("%T, %X\n", output, output)
}
uint64, FABC1234FABC
func Int32ToBinary
func Int32ToBinary(number int32) (str string)
Int32ToBinary returns a binary string representation of the input value of the int32 data type.
Int To Binary Example Functions Start
Output:Example
package main
import (
"fmt"
"github.com/kevinhng86/faiNumber-Go/faiNumber"
)
func main() {
var number int32 = -98745
output := faiNumber.Int32ToBinary(number)
fmt.Printf("%T, %v\n", output, output)
}
string, -11000000110111001
func Int32ToBinaryAsUnsigned
func Int32ToBinaryAsUnsigned(number int32) (str string)
Int32ToBinaryAsUnsigned returns a binary string representation of the input value of the int32 type,
This function treat the input value of type int32 as unsigned. This function returns the raw bit value
of the input value of the int32 data type and it doesn't interprete negative values as negative values.
Output:Example
package main
import (
"fmt"
"github.com/kevinhng86/faiNumber-Go/faiNumber"
)
func main() {
var number int32 = -1
output := faiNumber.Int32ToBinaryAsUnsigned(number)
fmt.Printf("%T, %v\n", output, output)
}
string, 11111111111111111111111111111111
func Int32ToDec
func Int32ToDec(number int32) (str string)
Int32ToDec returns a decimal string representation of the input value of the int32 data type.
Int To Decimal Example Functions Start
Output:Example
package main
import (
"fmt"
"github.com/kevinhng86/faiNumber-Go/faiNumber"
)
func main() {
var number int32 = -98745
output := faiNumber.Int32ToDec(number)
fmt.Printf("%T, %v\n", output, output)
}
string, -98745
func Int32ToHex
func Int32ToHex(number int32) (str string)
Int32ToHex returns a hexadecimal string representation of the input value of the int32 data type.
This function uses uppercase letters 'A' to 'F' for digit values >= 10.
Int To Hex Example Functions Start
Output:Example
package main
import (
"fmt"
"github.com/kevinhng86/faiNumber-Go/faiNumber"
)
func main() {
var number int32 = -98745
output := faiNumber.Int32ToHex(number)
fmt.Printf("%T, %v\n", output, output)
}
string, -181B9
func Int32ToOctal
func Int32ToOctal(number int32) (str string)
Int32ToOctal returns an octal string representation of the input value of the int32 data type.
Int To Octal Example Functions Start
Output:Example
package main
import (
"fmt"
"github.com/kevinhng86/faiNumber-Go/faiNumber"
)
func main() {
var number int32 = -98745
output := faiNumber.Int32ToOctal(number)
fmt.Printf("%T, %v\n", output, output)
}
string, -300671
func Int64ToBinary
func Int64ToBinary(number int64) (str string)
Int64ToBinary returns a binary string representation of the input value of the int64 data type.
Output:Example
package main
import (
"fmt"
"github.com/kevinhng86/faiNumber-Go/faiNumber"
)
func main() {
var number int64 = -45756787452134
output := faiNumber.Int64ToBinary(number)
fmt.Printf("%T, %v\n", output, output)
}
string, -1010011001110110010101000011111110100011100110
func Int64ToBinaryAsUnsigned
func Int64ToBinaryAsUnsigned(number int64) (str string)
Int64ToBinaryAsUnsigned returns a binary string representation of the input value of the int64 type,
This function treat the input value of type int64 as unsigned. This function returns the raw bit value
of the input value of the int64 data type and it doesn't interprete negative values as negative values.
Output:Example
package main
import (
"fmt"
"github.com/kevinhng86/faiNumber-Go/faiNumber"
)
func main() {
var number int64 = -1
output := faiNumber.Int64ToBinaryAsUnsigned(number)
fmt.Printf("%T, %v\n", output, output)
}
string, 1111111111111111111111111111111111111111111111111111111111111111
func Int64ToDec
func Int64ToDec(number int64) (str string)
Int64ToDec returns a decimal string representation of the input value of the int64 data type.
Output:Example
package main
import (
"fmt"
"github.com/kevinhng86/faiNumber-Go/faiNumber"
)
func main() {
var number int64 = -45756787452134
output := faiNumber.Int64ToDec(number)
fmt.Printf("%T, %v\n", output, output)
}
string, -45756787452134
func Int64ToHex
func Int64ToHex(number int64) (str string)
Int64ToHex returns a hexadecimal string representation of the input value of the int64 data type.
This function uses uppercase letters 'A' to 'F' for digit values >= 10.
Output:Example
package main
import (
"fmt"
"github.com/kevinhng86/faiNumber-Go/faiNumber"
)
func main() {
var number int64 = -45756787452134
output := faiNumber.Int64ToHex(number)
fmt.Printf("%T, %v\n", output, output)
}
string, -299D950FE8E6
func Int64ToOctal
func Int64ToOctal(number int64) (str string)
Int64ToOctal returns an octal string representation of the input value of the int64 data type.
Output:Example
package main
import (
"fmt"
"github.com/kevinhng86/faiNumber-Go/faiNumber"
)
func main() {
var number int64 = -45756787452134
output := faiNumber.Int64ToOctal(number)
fmt.Printf("%T, %v\n", output, output)
}
string, -1231662503764346
func MakeErrorEmptyString
func MakeErrorEmptyString(FunctionName string, input string) error
This function make error Empty String whenever needed. Error Empty String is for when the input string is empty.
faiNumber use error code 1 for all Empty String errors.
This function return a pointer to a new instance of FNError with the FNError.Message = "Empty string error" and the FNError.Code = 1. The FunctionName is use for the name of the function where the error occur. The input is the input that produced the error.
func MakeErrorInvalidFormat
func MakeErrorInvalidFormat(FunctionName string, input string) error
This function make error Invalid Format whenever needed. Error Invalid Format indicates that the parsing value is not the right format for the data type. e.g the input string is not a valid decimal number string when calling the function DecToInt32. All of faiNumber string parsing functions always check the input string to see whether if it is a valid numerical string type for the calling function even if an underflow or overflow error occur first. If the input string is not a valid string of the numerical type that the function required then the function will return error Invalid Format even if error overflow or error underflow occur first.
faiNumber use error code 2 for all Invalid Format errors.
This function return a pointer to a new instance of FNError with the FNError.Message = "Invalid format error" and the FNError.Code = 2. The FunctionName is use for the name of the function where the error occur. The input is the input that produced the error.
func MakeErrorOverflow
func MakeErrorOverflow(FunctionName string, input string) error
This function make error Overflow whenever needed. Error Overflow is produced when the input value is larger than the largest value that can be parsed to a data type. For signed type the value is larger than the largest positive value for the data type.
faiNumber use error code 4 for all Overflow errors.
This function return a pointer to a new instance of FNError with the FNError.Message = "Overflow error" and the FNError.Code = 4. The FunctionName is use for the name of the function where the error occur. The input is the input that produced the error.
func MakeErrorUnderflow
func MakeErrorUnderflow(FunctionName string, input string) error
This function make error Underflow whenever needed. Error Underflow is produced when the input value is smaller than the smallest negative value that can be parse to a signed data type.
faiNumber use error code 3 for all Underflow errors.
This function return a pointer to a new instance of FNError with the FNError.Message = "Underflow error" and the FNError.Code = 3. The FunctionName is use for the name of the function where the error occur. The input is the input that produced the error.
func OctalToInt32
func OctalToInt32(str string) (int32, error)
OctalToInt32 parse the input string as a signed octal integer string to an int32 value. The input string can begin with a leading sign: "+" or "-" to determine whether the input string represent a positive or negative value.
The errors that OctalToInt32 returns are type *FNError and include FNError.Input = str, FNError.FromFunction = "faiNumber.OctalToInt32", FNError.Message = Error message, and FNError.Code = The error code. The error code and error message is different depend on which type of error.
If str is empty then Error Code 1 is return. faiNumber classified this as an Empty String Error.
If str contains invalid octal digits then Error Code 2 is return. faiNumber classfied this as an Invalid Format Error.
If the value corresponding to str is smaller than the minimum value an int32 can hold then Error Code 3 is return. faiNumber classified this and an Underflow Error.
If the value corresponding to str is larger than the maximum value an int32 can hold then Error Code 4 is return.
faiNumber classfied this as an Overflow Error.
Octal To Int Example Functions Start
Output:Example
package main
import (
"fmt"
"github.com/kevinhng86/faiNumber-Go/faiNumber"
)
func main() {
str := "-7434741"
output, err := faiNumber.OctalToInt32(str)
if err != nil {
fmt.Println("Error handling")
}
fmt.Printf("%T, %o\n", output, output)
}
int32, -7434741
func OctalToInt64
func OctalToInt64(str string) (int64, error)
OctalToInt64 parse the input string as a signed octal integer string to an int64 value. The input string can begin with a leading sign: "+" or "-" to determine whether the input string represent a positive or negative value.
The errors that OctalToInt64 returns are type *FNError and include FNError.Input = str, FNError.FromFunction = "faiNumber.OctalToInt64", FNError.Message = Error message, and FNError.Code = The error code. The error code and error message is different depend on which type of error.
If str is empty then Error Code 1 is return. faiNumber classified this as an Empty String Error.
If str contains invalid octal digits then Error Code 2 is return. faiNumber classfied this as an Invalid Format Error.
If the value corresponding to str is smaller than the minimum value an int64 can hold then Error Code 3 is return. faiNumber classified this and an Underflow Error.
If the value corresponding to str is larger than the maximum value an int64 can hold then Error Code 4 is return.
faiNumber classfied this as an Overflow Error.
Output:Example
package main
import (
"fmt"
"github.com/kevinhng86/faiNumber-Go/faiNumber"
)
func main() {
str := "-5434741567"
output, err := faiNumber.OctalToInt64(str)
if err != nil {
fmt.Println("Error handling")
}
fmt.Printf("%T, %o\n", output, output)
}
int64, -5434741567
func OctalToUInt32
func OctalToUInt32(str string) (uint32, error)
OctalToUInt32 parse the input string as an unsigned octal integer string to an uint32 value. Sign parsing is not permitted. If there is a sign prefix then the function will yield an Invalid Format Error.
The errors that OctalToUInt32 returns are type *FNError and include FNError.Input = str, FNError.FromFunction = "faiNumber.OctalToUInt32", FNError.Message = Error message, and FNError.Code = The error code. The error code and error message is different depend on which type of error.
If str is empty then Error Code 1 is return. faiNumber classified this as an Empty String Error.
If str contains invalid octal digits then Error Code 2 is return. faiNumber classfied this as an Invalid Format Error.
If the value corresponding to str is larger than the maximum value an uint32 can hold then Error Code 4 is return.
faiNumber classfied this as an Overflow Error.
Output:Example
package main
import (
"fmt"
"github.com/kevinhng86/faiNumber-Go/faiNumber"
)
func main() {
str := "3741471"
output, err := faiNumber.OctalToUInt32(str)
if err != nil {
fmt.Println("Error handling")
}
fmt.Printf("%T, %o\n", output, output)
}
uint32, 3741471
func OctalToUInt64
func OctalToUInt64(str string) (uint64, error)
OctalToUInt64 parse the input string as an unsigned octal integer string to an uint64 value. Sign parsing is not permitted. If there is a sign prefix then the function will yield an Invalid Format Error.
The errors that OctalToUInt64 returns are type *FNError and include FNError.Input = str, FNError.FromFunction = "faiNumber.OctalToUInt64", FNError.Message = Error message, and FNError.Code = The error code. The error code and error message is different depend on which type of error.
If str is empty then Error Code 1 is return. faiNumber classified this as an Empty String Error.
If str contains invalid octal digits then Error Code 2 is return. faiNumber classfied this as an Invalid Format Error.
If the value corresponding to str is larger than the maximum value an uint64 can hold then Error Code 4 is return.
faiNumber classfied this as an Overflow Error.
Output:Example
package main
import (
"fmt"
"github.com/kevinhng86/faiNumber-Go/faiNumber"
)
func main() {
str := "7534534754"
output, err := faiNumber.OctalToUInt64(str)
if err != nil {
fmt.Println("Error handling")
}
fmt.Printf("%T, %o\n", output, output)
}
uint64, 7534534754
func UInt32ToBinary
func UInt32ToBinary(number uint32) (str string)
UInt32ToBinary returns a binary string representation of the input value of the uint32 data type.
Output:Example
package main
import (
"fmt"
"github.com/kevinhng86/faiNumber-Go/faiNumber"
)
func main() {
var number uint32 = 35478567
output := faiNumber.UInt32ToBinary(number)
fmt.Printf("%T, %v\n", output, output)
}
string, 10000111010101110000100111
func UInt32ToDec
func UInt32ToDec(number uint32) (str string)
UInt32ToDec returns a decimal string representation of the input value of the uint32 data type.
Output:Example
package main
import (
"fmt"
"github.com/kevinhng86/faiNumber-Go/faiNumber"
)
func main() {
var number uint32 = 35478567
output := faiNumber.UInt32ToDec(number)
fmt.Printf("%T, %v\n", output, output)
}
string, 35478567
func UInt32ToHex
func UInt32ToHex(number uint32) (str string)
UInt32ToHex returns a hexadecimal string representation of the input value of the uint32 data type.
This function uses uppercase letters 'A' to 'F' for digit values >= 10.
Output:Example
package main
import (
"fmt"
"github.com/kevinhng86/faiNumber-Go/faiNumber"
)
func main() {
var number uint32 = 35478567
output := faiNumber.UInt32ToHex(number)
fmt.Printf("%T, %v\n", output, output)
}
string, 21D5C27
func UInt32ToOctal
func UInt32ToOctal(number uint32) (str string)
UInt32ToOctal returns an octal string representation of the input value of the uint32 data type.
Output:Example
package main
import (
"fmt"
"github.com/kevinhng86/faiNumber-Go/faiNumber"
)
func main() {
var number uint32 = 35478567
output := faiNumber.UInt32ToOctal(number)
fmt.Printf("%T, %v\n", output, output)
}
string, 207256047
func UInt64ToBinary
func UInt64ToBinary(number uint64) (str string)
UInt64ToBinary returns a binary string representation of the input value of the uint64 data type.
Output:Example
package main
import (
"fmt"
"github.com/kevinhng86/faiNumber-Go/faiNumber"
)
func main() {
var number uint64 = 547456745412
output := faiNumber.UInt64ToBinary(number)
fmt.Printf("%T, %v\n", output, output)
}
string, 111111101110110111101101111111111000100
func UInt64ToDec
func UInt64ToDec(number uint64) (str string)
UInt64ToDec returns a decimal string representation of the input value of the uint64 data type.
Output:Example
package main
import (
"fmt"
"github.com/kevinhng86/faiNumber-Go/faiNumber"
)
func main() {
var number uint64 = 547456745412
output := faiNumber.UInt64ToDec(number)
fmt.Printf("%T, %v\n", output, output)
}
string, 547456745412
func UInt64ToHex
func UInt64ToHex(number uint64) (str string)
UInt64ToHex returns a hexadecimal string representation of the input value of the uint64 data type.
This function uses uppercase letters 'A' to 'F' for digit values >= 10.
Output:Example
package main
import (
"fmt"
"github.com/kevinhng86/faiNumber-Go/faiNumber"
)
func main() {
var number uint64 = 547456745412
output := faiNumber.UInt64ToHex(number)
fmt.Printf("%T, %v\n", output, output)
}
string, 7F76F6FFC4
func UInt64ToOctal
func UInt64ToOctal(number uint64) (str string)
UInt64ToOctal returns an octal string representation of the input value of the uint64 data type.
Output:Example
package main
import (
"fmt"
"github.com/kevinhng86/faiNumber-Go/faiNumber"
)
func main() {
var number uint64 = 547456745412
output := faiNumber.UInt64ToOctal(number)
fmt.Printf("%T, %v\n", output, output)
}
string, 7756675577704
Types
type FNError
type FNError struct { FromFunction string // The function where the error is produced e.g DecToInt32, DecToUInt32, DecToInt64, DecToUInt64. Input string // The input string. Message string // The error message. Code int // The error code. }
FNError is use to record errors when they occur. A pointer to a new instance of FNError is returned whenever
an error occur.
Error Example Start
Output:Example
package main
import (
"fmt"
"github.com/kevinhng86/faiNumber-Go/faiNumber"
)
func main() {
str := "1A"
if _, err := faiNumber.DecToInt32(str); err != nil {
e := err.(*faiNumber.FNError)
fmt.Println("From Function:", e.FromFunction)
fmt.Println("Input:", e.Input)
fmt.Println("Error Message:", e.Message)
fmt.Println("Error Code:", e.Code)
fmt.Println(err)
}
}
From Function: faiNumber.DecToInt32
Input: 1A
Error Message: Invalid format error
Error Code: 2
Error from function faiNumber.DecToInt32. Error code: 2. Input value: "1A". Message: Invalid format error.
func (*FNError) Error
func (e *FNError) Error() string
An implement of the Error() method for *FNError. A string representation of the error is return.