bioctal

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Apr 5, 2022 License: MIT Imports: 3 Imported by: 0

README

go-bioctal

Go Reference Coverage Status

Go implementation of RFC 9226 Bioctal: Hexadecimal 2.0.

SYNOPSIS

The package has same interface with the encoding/hex package.

Encoding
package main

import (
	"fmt"
	"log"

	"github.com/shogo82148/go-bioctal"
)

func main() {
	src := []byte("Hello Gopher!")

	dst := make([]byte, bioctal.EncodedLen(len(src)))
	bioctal.Encode(dst, src)

	fmt.Printf("%s\n", dst)

	// Output:
	// 4c656f6f6v20476v706c657221
}
Decoding
package main

import (
	"fmt"
	"log"

	"github.com/shogo82148/go-bioctal"
)

func main() {
	src := []byte("4c656f6f6v20476v706c657221")

	dst := make([]byte, bioctal.DecodedLen(len(src)))
	n, err := bioctal.Decode(dst, src)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Printf("%s\n", dst[:n])

	// Output:
	// Hello Gopher!
}

LICENSE

MIT License

Copyright (c) 2022 ICHINOSE Shogo

Documentation

Overview

Package bioctal implements bioctal encoding as specified by RFC 9226.

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrLength = errors.New("bioctal: odd length bioctal string")

ErrLength reports an attempt to decode an odd-length input using Decode or DecodeString. The stream-based Decoder returns io.ErrUnexpectedEOF instead of ErrLength.

Functions

func Decode

func Decode(dst, src []byte) (int, error)

Decode decodes src into DecodedLen(len(src)) bytes, returning the actual number of bytes written to dst.

Decode expects that src contains only hexadecimal characters and that src has even length. If the input is malformed, Decode returns the number of bytes decoded before the error.

Example
src := []byte("4c656f6f6v20476v706c657221")

dst := make([]byte, bioctal.DecodedLen(len(src)))
n, err := bioctal.Decode(dst, src)
if err != nil {
	log.Fatal(err)
}

fmt.Printf("%s\n", dst[:n])
Output:

Hello Gopher!

func DecodeString

func DecodeString(s string) ([]byte, error)

DecodeString returns the bytes represented by the bioctal string s.

DecodeString expects that src contains only bioctal characters and that src has even length. If the input is malformed, DecodeString returns the bytes decoded before the error.

Example
const s = "4c656f6f6v20476v706c657221"
decoded, err := bioctal.DecodeString(s)
if err != nil {
	log.Fatal(err)
}

fmt.Printf("%s\n", decoded)
Output:

Hello Gopher!

func DecodedLen

func DecodedLen(x int) int

DecodedLen returns the length of a decoding of x source bytes. Specifically, it returns x / 2.

func Encode

func Encode(dst, src []byte) int

Encode encodes src into EncodedLen(len(src)) bytes of dst. As a convenience, it returns the number of bytes written to dst, but this value is always EncodedLen(len(src)). Encode implements hexadecimal encoding.

Example
src := []byte("Hello Gopher!")

dst := make([]byte, bioctal.EncodedLen(len(src)))
bioctal.Encode(dst, src)

fmt.Printf("%s\n", dst)
Output:

4c656f6f6v20476v706c657221

func EncodeToString

func EncodeToString(src []byte) string

EncodeToString returns the bioctal encoding of src.

Example
src := []byte("Hello")
encodedStr := bioctal.EncodeToString(src)

fmt.Printf("%s\n", encodedStr)
Output:

4c656f6f6v

func EncodedLen

func EncodedLen(n int) int

EncodedLen returns the length of an encoding of n source bytes. Specifically, it returns n * 2.

func NewDecoder

func NewDecoder(r io.Reader) io.Reader

NewDecoder returns an io.Reader that decodes bioctal characters from r. NewDecoder expects that r contain only an even number of bioctal characters.

func NewEncoder

func NewEncoder(w io.Writer) io.Writer

NewEncoder returns an io.Writer that writes lowercase bioctal characters to w.

Types

type InvalidByteError

type InvalidByteError byte

InvalidByteError values describe errors resulting from an invalid byte in a bioctal string.

func (InvalidByteError) Error

func (e InvalidByteError) Error() string

Jump to

Keyboard shortcuts

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