---
source: https://iinventedlight.com/library/notes/go-encoding-hex.md
title: "encoding/hex: hexadecimal encoding and decoding"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "BSD-3-Clause (Go); BSD-3-Clause (Go); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "Summary by iinventedlight (AI-assisted). Sources: Go, encoding/hex package reference; Go, encoding/hex/hex.go source. BSD-3-Clause (https://go.dev/LICENSE). Copyright The Go Authors."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-23
status: published
approved_by: "Cole Withington, 2026-11-27 14:30 PT: \"Yes, publish all of that now\""
checked_on: 2026-11-23
as_of: 2026-11-23
confidence: medium
untested: "Not reproduced (2026-11-23). Official docs and source files were read; no test suite, server, or API call was run for this note."
category: coding-references
topic_area: "code-systems: go-encoding-hex"
tags: [go, encoding, hex, hexadecimal]
client: Librarian
sources:
  - id: S1
    title: "encoding/hex package reference"
    publisher: Go
    url: https://pkg.go.dev/encoding/hex
    accessed: 2026-11-23
    license: "BSD-3-Clause"
  - id: S2
    title: "encoding/hex/hex.go source"
    publisher: Go
    url: https://go.dev/src/encoding/hex/hex.go
    accessed: 2026-11-23
    license: "BSD-3-Clause"
---
# encoding/hex: hexadecimal encoding and decoding
**Answer:** Package hex encodes and decodes hexadecimal data. Encode writes two encoded bytes for each source byte, since EncodedLen returns n * 2. Decode needs an even-length string of hex characters. EncodedLen is n * 2. DecodedLen is x / 2.

## Encode and decode

The hex package is there to encode and decode hexadecimal data.

Encode writes into dst, using EncodedLen(len(src)) bytes.

The count it returns is always that same EncodedLen value.

EncodedLen is the size of an encoding of n bytes, which is n * 2.

Decode writes into a buffer of length DecodedLen(len(src)) and reports how many bytes it wrote.

The input must be even in length and contain only hexadecimal characters.

On a bad input, Decode reports how far it got before the error.

## Length errors and helpers

DecodedLen is the size of a decoding of x bytes, which is x / 2.

ErrLength is the error for an odd-length input to Decode or DecodeString.

The stream-based Decoder returns io.ErrUnexpectedEOF instead of ErrLength.

InvalidByteError is the error type for a bad byte in a hex string.

AppendEncode appends the hexadecimally encoded src to dst and returns the extended buffer.

## See also

- [go-encoding-base64](https://iinventedlight.com/library/notes/go-encoding-base64/)
- [go-encoding-csv](https://iinventedlight.com/library/notes/go-encoding-csv/)
- [go-bytes](https://iinventedlight.com/library/notes/go-bytes/)

## Claims

- id: C1
  claim: The package implements hexadecimal encoding and decoding.
  source: S1
  evidence: "implements hexadecimal encoding and decoding"

- id: C2
  claim: EncodedLen returns n * 2.
  source: S1
  evidence: "it returns n * 2"

- id: C3
  claim: Decode expects even-length hexadecimal input.
  source: S1
  evidence: "src has even length"

- id: C4
  claim: DecodedLen returns x / 2.
  source: S2
  evidence: "it returns x / 2"

- id: C5
  claim: ErrLength reports an odd-length decode attempt.
  source: S2
  evidence: "decode an odd-length input"

- id: C6
  claim: InvalidByteError describes an invalid byte in a hex string.
  source: S2
  evidence: "invalid byte in a hex string"

## Sources

- [S1] encoding/hex package reference. Go. https://pkg.go.dev/encoding/hex . License: BSD-3-Clause. Accessed 2026-11-23.

- [S2] encoding/hex/hex.go source. Go. https://go.dev/src/encoding/hex/hex.go . License: BSD-3-Clause. Accessed 2026-11-23.

License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Go, encoding/hex package reference; Go, encoding/hex/hex.go source. BSD-3-Clause (https://go.dev/LICENSE). Copyright The Go Authors. Text was condensed and changed from the originals.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
