Documentation ¶
Overview ¶
Package byter provides functions that manipulate or return byte arrays.
Index ¶
- Constants
- Variables
- func BOM() []byte
- func Decode(c *charmap.Charmap, s string) ([]byte, error)
- func Encode(c *charmap.Charmap, s string) ([]byte, error)
- func HexDecode(s string) ([]byte, error)
- func HexEncode(s string) []byte
- func MakeBytes() []byte
- func Mark(b []byte) []byte
- func TrimEOF(b []byte) []byte
Examples ¶
Constants ¶
View Source
const ( SUB = 26 // SUB is the ASCII control code for substitute. DosSUB = 8594 // DosSub is the Unicode for the right-arrow. SymbolSUB = 9242 // SymbolSUB is the Unicode for the substitute character. )
Variables ¶
View Source
var ( ErrCharmap = errors.New("the c charmap cannot be nil") ErrUTF8 = errors.New("string cannot encode to utf-8") )
Functions ¶
func BOM ¶
func BOM() []byte
BOM is the UTF-8 byte order mark prefix.
Example ¶
package main import ( "fmt" "github.com/bengarrett/retrotxtgo/byter" ) func main() { fmt.Printf("#%x", byter.BOM()) }
Output: #efbbbf
func Decode ¶
Decode a string using the character map.
Example ¶
package main import ( "fmt" "github.com/bengarrett/retrotxtgo/byter" "golang.org/x/text/encoding/charmap" ) func main() { s := "\xCD\xB9\xB2\xCC\xCD" p, _ := byter.Decode(charmap.CodePage437, s) fmt.Printf("%s\n", p) p, _ = byter.Decode(charmap.ISO8859_1, s) fmt.Printf("%s\n", p) }
Output: ═╣▓╠═ ͹²ÌÍ
func Encode ¶
Encode the string using the character map.
Example ¶
package main import ( "fmt" "github.com/bengarrett/retrotxtgo/byter" "golang.org/x/text/encoding/charmap" ) func main() { p, _ := byter.Encode(charmap.CodePage437, "═╣▓╠═") fmt.Printf("%X\n", p) p, _ = byter.Encode(charmap.ISO8859_1, "hello") fmt.Printf("%s (%X)\n", p, p) p, _ = byter.Encode(charmap.CodePage1140, "hello") fmt.Printf("%X\n", p) // not ascii compatible }
Output: CDB9B2CCCD hello (68656C6C6F) 8885939396
func HexDecode ¶
HexDecode decodes a hexadecimal string into bytes.
Example ¶
package main import ( "fmt" "github.com/bengarrett/retrotxtgo/byter" ) func main() { b, _ := byter.HexDecode("6F6B") fmt.Printf("%s\n", b) fmt.Printf("%d\n", b) }
Output: ok [111 107]
func HexEncode ¶
HexEncode encodes a string into hexadecimal bytes.
Example ¶
package main import ( "fmt" "github.com/bengarrett/retrotxtgo/byter" ) func main() { b := byter.HexEncode("ok") fmt.Printf("#%s\n", b) }
Output: #6f6b
func MakeBytes ¶
func MakeBytes() []byte
MakeBytes generates a 256 character or 8-bit container ready to hold legacy code point values.
Example ¶
package main import ( "fmt" "github.com/bengarrett/retrotxtgo/byter" ) func main() { fmt.Printf("%d", len(byter.MakeBytes())) }
Output: 256
func TrimEOF ¶
TrimEOF will cut text at the first occurrence of the SUB character. The SUB is used by DOS and CP/M as an end-of-file marker.
Example ¶
package main import ( "fmt" "github.com/bengarrett/retrotxtgo/byter" ) func main() { fmt.Printf("%q", byter.TrimEOF([]byte("hello world\x1a"))) }
Output: "hello world"
Types ¶
This section is empty.
Click to show internal directories.
Click to hide internal directories.