package hex
Import Path
encoding/hex (on go.dev)
Dependency Relation
imports 4 packages, and imported by 2 packages
Involved Source Files
Package hex implements hexadecimal encoding and decoding.
Code Examples
package main
import (
"encoding/hex"
"fmt"
"log"
)
func main() {
src := []byte("48656c6c6f20476f7068657221")
dst := make([]byte, hex.DecodedLen(len(src)))
n, err := hex.Decode(dst, src)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s\n", dst[:n])
}
package main
import (
"encoding/hex"
"fmt"
"log"
)
func main() {
const s = "48656c6c6f20476f7068657221"
decoded, err := hex.DecodeString(s)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s\n", decoded)
}
package main
import (
"encoding/hex"
"fmt"
)
func main() {
content := []byte("Go is an open source programming language.")
fmt.Printf("%s", hex.Dump(content))
}
package main
import (
"encoding/hex"
"os"
)
func main() {
lines := []string{
"Go is an open source programming language.",
"\n",
"We encourage all Go users to subscribe to golang-announce.",
}
stdoutDumper := hex.Dumper(os.Stdout)
defer stdoutDumper.Close()
for _, line := range lines {
stdoutDumper.Write([]byte(line))
}
}
package main
import (
"encoding/hex"
"fmt"
)
func main() {
src := []byte("Hello Gopher!")
dst := make([]byte, hex.EncodedLen(len(src)))
hex.Encode(dst, src)
fmt.Printf("%s\n", dst)
}
package main
import (
"encoding/hex"
"fmt"
)
func main() {
src := []byte("Hello")
encodedStr := hex.EncodeToString(src)
fmt.Printf("%s\n", encodedStr)
}
Package-Level Type Names (total 4, in which 1 are exported)
Package-Level Functions (total 12, in which 10 are exported)
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.
DecodedLen returns the length of a decoding of x source bytes.
Specifically, it returns x / 2.
DecodeString returns the bytes represented by the hexadecimal string s.
DecodeString expects that src contains only hexadecimal
characters and that src has even length.
If the input is malformed, DecodeString returns
the bytes decoded before the error.
Dump returns a string that contains a hex dump of the given data. The format
of the hex dump matches the output of `hexdump -C` on the command line.
Dumper returns a WriteCloser that writes a hex dump of all written data to
w. The format of the dump matches the output of `hexdump -C` on the command
line.
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.
EncodedLen returns the length of an encoding of n source bytes.
Specifically, it returns n * 2.
EncodeToString returns the hexadecimal encoding of src.
NewDecoder returns an io.Reader that decodes hexadecimal characters from r.
NewDecoder expects that r contain only an even number of hexadecimal characters.
NewEncoder returns an io.Writer that writes lowercase hexadecimal characters to w.
Package-Level Variables (only one, which is exported)
ErrLength reports an attempt to decode an odd-length input
using Decode or DecodeString.
The stream-based Decoder returns io.ErrUnexpectedEOF instead of ErrLength.
Package-Level Constants (total 2, neither is exported)
The pages are generated with Golds v0.4.2. (GOOS=darwin GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds. |