numx

package
v0.4.3 Latest Latest
Warning

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

Go to latest
Published: Mar 16, 2025 License: BSD-3-Clause Imports: 0 Imported by: 0

README


This utility package for converting between big-endian byte slices and various numeric types such as uint64, uint32, uint16, uint8, and their signed counterparts. It provides efficient and simple functions to handle big-endian encoding/decoding of numbers and supports conversion both from and to byte slices.

Features:

  • Convert between byte slices and uint64, uint32, uint16, and uint8.
  • Handle signed integers with conversion functions for int64, int32, int16, and int8.
  • Support conversion from integers to big-endian byte slices.
  • Optimized for performance with minimal memory overhead.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func B1

func B1(n uint8) []byte

B1 converts a uint8 number into a single-byte slice.

func B2

func B2(n uint16) []byte

B2 converts a uint16 number into a big-endian byte slice of length 2.

func B4

func B4(n uint32) []byte

B4 converts a uint32 number into a big-endian byte slice of length 4.

Example
package main

import (
	"fmt"

	"github.com/exonlabs/go-utils/pkg/abc/numx"
)

func main() {
	n := uint32(16909060)
	fmt.Printf("%x\n", numx.B4(n))
}
Output:

01020304

func B8

func B8(n uint64) []byte

B8 converts a uint64 number into a big-endian byte slice of length 8.

Example
package main

import (
	"fmt"

	"github.com/exonlabs/go-utils/pkg/abc/numx"
)

func main() {
	n := uint64(72623859790382856)
	fmt.Printf("%x\n", numx.B8(n))
}
Output:

0102030405060708

func I16

func I16(b []byte) int16

I16 converts a big-endian byte slice to an int16 number.

func I32

func I32(b []byte) int32

I32 converts a big-endian byte slice to an int32 number.

func I64

func I64(b []byte) int64

I64 converts a big-endian byte slice to an int64 number. It processes up to the first 8 bytes and handles signed integers.

Example
package main

import (
	"fmt"

	"github.com/exonlabs/go-utils/pkg/abc/numx"
)

func main() {
	b := []byte{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}
	fmt.Println(numx.I64(b)) // Signed negative
}
Output:

-1

func I8

func I8(b []byte) int8

I8 converts a big-endian byte slice to an int8 number.

func Q1

func Q1(n int8) []byte

Q1 converts an int8 number into a single-byte slice.

func Q2

func Q2(n int16) []byte

Q2 converts an int16 number into a big-endian byte slice of length 2.

func Q4

func Q4(n int32) []byte

Q4 converts an int32 number into a big-endian byte slice of length 4.

func Q8

func Q8(n int64) []byte

Q8 converts an int64 number into a big-endian byte slice of length 8. It handles both positive and negative numbers using 2's complement.

Example
package main

import (
	"fmt"

	"github.com/exonlabs/go-utils/pkg/abc/numx"
)

func main() {
	n := int64(-1)
	fmt.Printf("%x\n", numx.Q8(n))
}
Output:

ffffffffffffffff

func U16

func U16(b []byte) uint16

U16 converts a big-endian byte slice to a uint16 number.

func U32

func U32(b []byte) uint32

U32 converts a big-endian byte slice to a uint32 number.

Example
package main

import (
	"fmt"

	"github.com/exonlabs/go-utils/pkg/abc/numx"
)

func main() {
	b := []byte{0x01, 0x02, 0x03, 0x04}
	fmt.Println(numx.U32(b))
}
Output:

16909060

func U64

func U64(b []byte) uint64

U64 converts a big-endian byte slice to a uint64 number. It processes up to the first 8 bytes of the slice.

Example
package main

import (
	"fmt"

	"github.com/exonlabs/go-utils/pkg/abc/numx"
)

func main() {
	b := []byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}
	fmt.Println(numx.U64(b))
}
Output:

72623859790382856

func U8

func U8(b []byte) uint8

U8 converts a big-endian byte slice to a uint8 number.

Types

This section is empty.

Jump to

Keyboard shortcuts

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