Library contents

Go Encoding.EncodeToString

Base64 represents a sequence of bytes as ASCII text that a text-only field can carry.

A simple example

Encoding.EncodeToString

  1. Start with "cat" encoded as UTF-8
  2. Do this Encode its three bytes as Base64
  3. Result "Y2F0"
Base64 represents these three bytes with four printable characters. Decoding restores cat.

Syntax

Go syntax
base64.StdEncoding.EncodeToString([]byte("tea"))
Run these examples in Go

Save one example at a time as main.go. Use Go 1.21 or later.

go run main.go

Encode a short delivery note for a text field

A system can carry a short payload only in a text field. Encode the original bytes into the alphabet the receiving field accepts, and distinguish that transport encoding from the underlying data.

ResultdGVh
main.go
package main

import (
    "fmt"
    "encoding/base64"
)

func main() {
    fmt.Println(base64.StdEncoding.EncodeToString([]byte("tea")))
}
Output
dGVh

The three UTF-8 bytes for tea become the four Base64 characters dGVh. Decoding them recovers the original bytes.

Advertisements

Encode a parcel marker

A parcel terminal needs a text version of its three marker bytes. Encode the supplied ASCII bytes and read the four-character Base64 group. These inputs occupy a complete three-byte block.

GivenBOX
Worked resultQk9Y

Store an exhibit tag

A catalog stores three literal tag bytes in a Base64 field. Encode the supplied ASCII bytes and read the four-character Base64 group. These inputs occupy a complete three-byte block.

GivenART
Worked resultQVJU

What to watch for

Base64 is reversible encoding. It offers no confidentiality.

Documentation for Encoding.EncodeToString