hexa

package
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Jan 3, 2025 License: LGPL-3.0 Imports: 5 Imported by: 0

Documentation

Overview

Package hexa provides rudimental hexadecimal conversion functions.

Index

Examples

Constants

View Source
const (
	X    = "X"   // common
	Hash = "#"   // cascading style sheets colors
	Doll = "$"   // retro microcomputers
	U    = "U+"  // unicode
	Zero = "0X"  // linux and unix C syntax
	Esc  = "\\X" // escape
)

Hexadecimal prefix identifiers.

Variables

This section is empty.

Functions

func Parse

func Parse(b Base, vals ...string) []int64

Parse converts the provided strings to based unsigned ints. If a string is not a valid integer then the value is -1.

Example
package main

import (
	"fmt"

	"github.com/bengarrett/retrotxtgo/cmd/hexa"
)

func main() {
	n := hexa.Parse(16, "0F", "01", "FF", "Z")
	fmt.Println(n)

	n = hexa.Parse(10, "15", "1", "255", "abc")
	fmt.Println(n)
}
Output:

[15 1 255 -1]
[15 1 255 -1]

func Parser

func Parser(w io.Writer, b Base, vals ...string) error

Parser writes the hexadecimal values of the provided decimal strings. Only the results are written and are separated by a space. If a string is not a hexadecimal number then the value is printed as "NaN".

Example
package main

import (
	"os"

	"github.com/bengarrett/retrotxtgo/cmd/hexa"
)

func main() {
	_ = hexa.Parser(os.Stdout, 16, "0F", "01", "FF", "Z")
}
Output:

15 1 255 NaN

func TrimIdent

func TrimIdent(s string) string

TrimIdent trims the hexadecimal prefix identifiers and returns the hexadecimal value as an upper case string.

Example
package main

import (
	"fmt"

	"github.com/bengarrett/retrotxtgo/cmd/hexa"
)

func main() {
	// unicode example
	s := hexa.TrimIdent("U+00A9")
	fmt.Println(s)

	// retro hex example
	s = hexa.TrimIdent("$0F")
	fmt.Println(s)
}
Output:

00A9
0F

func TrimIndents

func TrimIndents(vals ...string) []string

TrimIndents trims the hexadecimal prefix identifiers and returns the hexadecimal values as upper case strings.

Example
package main

import (
	"fmt"

	"github.com/bengarrett/retrotxtgo/cmd/hexa"
)

func main() {
	s := hexa.TrimIndents("U+00A9", "$0F")
	fmt.Println(s)
}
Output:

[00A9 0F]

func TrimNCR

func TrimNCR(s string) string

TrimNCR trims the numeric character reference prefix and suffix, and returns a decimal integer as a string. If the string is not in NCR syntax, then the original string is returned.

Example
package main

import (
	"fmt"

	"github.com/bengarrett/retrotxtgo/cmd/hexa"
)

func main() {
	s := hexa.TrimNCR("©")
	fmt.Println(s)
}
Output:

169

func Writer

func Writer(w io.Writer, b Base, vals ...string) error

Writer the hexadecimal values of the provided decimal strings. If a string is not a hexadecimal number then the value is printed as "invalid".

Example
package main

import (
	"os"

	"github.com/bengarrett/retrotxtgo/cmd/hexa"
)

func main() {
	// hexadecimal example
	_ = hexa.Writer(os.Stdout, 16, "0F", "01", "FF", "Z")

	// decimal example
	_ = hexa.Writer(os.Stdout, 10, "15", "1", "255", "abc")
}
Output:

0F = 15  01 = 1  FF = 255  Z = invalid
15 = F  1 = 1  255 = FF  ABC = invalid

Types

type Base

type Base int

Base is the number base system for the conversion.

const (
	Base10 Base = 10 // Base10 is the decimal number base system.
	Base16 Base = 16 // Base16 is the hexadecimal number base system.
)

Jump to

Keyboard shortcuts

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