# std.encoding

Binary-to-text encodings: base64, URL-safe base64, and hex.

```tea
use encoding from "std.encoding"
```

Examples may be fragments requiring the module import or additional setup.

## std.encoding.base64_encode

```tea
pub def base64_encode(data: List[Int]) -> String
```

Encode bytes as standard base64 text.

```tea
use encoding from "std.encoding"

@println(encoding.base64_encode([102, 111, 111])) # Zm9v
```

## std.encoding.base64_encode_text

```tea
pub def base64_encode_text(text: String) -> String
```

Encode UTF-8 text as standard base64 text.

```tea
use encoding from "std.encoding"

@println(encoding.base64_encode_text("hello")) # aGVsbG8=
```

## std.encoding.try_base64_decode

```tea
pub def try_base64_decode(text: String) -> List[Int] ! EncodingError
```

Decode standard base64 text into bytes, throwing on invalid input.

```tea
use encoding from "std.encoding"

def demonstrate() -> Void
  try encoding.try_base64_decode("!!!") catch err
    case is encoding.EncodingError.InvalidBase64
      @println(`Invalid input: ${err.input}`)
      return
    case _
      @panic("unexpected encoding error")
  end
  return
end

demonstrate()
```

## std.encoding.base64_decode

```tea
pub def base64_decode(text: String) -> List[Int]
```

Decode standard base64 text into bytes, panicking on invalid input.

```tea
use encoding from "std.encoding"

const encoded = encoding.base64_encode_text("foo")
@println(encoding.base64_decode(encoded)) # [102, 111, 111]
```

## std.encoding.base64url_encode

```tea
pub def base64url_encode(data: List[Int]) -> String
```

Encode bytes as URL-safe base64 text without padding.

```tea
use encoding from "std.encoding"

@println(encoding.base64url_encode([251, 255])) # -_8
```

## std.encoding.base64url_encode_text

```tea
pub def base64url_encode_text(text: String) -> String
```

Encode UTF-8 text as URL-safe base64 text without padding.

```tea
use encoding from "std.encoding"

@println(encoding.base64url_encode_text("hello")) # aGVsbG8
```

## std.encoding.try_base64url_decode

```tea
pub def try_base64url_decode(text: String) -> List[Int] ! EncodingError
```

Decode URL-safe base64 text into bytes, throwing on invalid input.

```tea
use encoding from "std.encoding"

def demonstrate() -> Void
  try encoding.try_base64url_decode("!!!") catch err
    case is encoding.EncodingError.InvalidBase64
      @println(`Invalid input: ${err.input}`)
      return
    case _
      @panic("unexpected encoding error")
  end
  return
end

demonstrate()
```

## std.encoding.base64url_decode

```tea
pub def base64url_decode(text: String) -> List[Int]
```

Decode URL-safe base64 text into bytes, panicking on invalid input.

```tea
use encoding from "std.encoding"

@println(encoding.base64url_decode("-_8")) # [251, 255]
```

## std.encoding.hex_encode

```tea
pub def hex_encode(data: List[Int]) -> String
```

Encode bytes as lowercase hexadecimal text.

```tea
use encoding from "std.encoding"

@println(encoding.hex_encode([0, 255, 16])) # 00ff10
```

## std.encoding.hex_encode_text

```tea
pub def hex_encode_text(text: String) -> String
```

Encode UTF-8 text as lowercase hexadecimal text.

```tea
use encoding from "std.encoding"

@println(encoding.hex_encode_text("foo")) # 666f6f
```

## std.encoding.try_hex_decode

```tea
pub def try_hex_decode(text: String) -> List[Int] ! EncodingError
```

Decode hexadecimal text into bytes, throwing on invalid input.

```tea
use encoding from "std.encoding"

def demonstrate() -> Void
  try encoding.try_hex_decode("zz") catch err
    case is encoding.EncodingError.InvalidHex
      @println(`Invalid input: ${err.input}`)
      return
    case _
      @panic("unexpected encoding error")
  end
  return
end

demonstrate()
```

## std.encoding.hex_decode

```tea
pub def hex_decode(text: String) -> List[Int]
```

Decode hexadecimal text into bytes, panicking on invalid input.

```tea
use encoding from "std.encoding"

@println(encoding.hex_decode("00FF10")) # [0, 255, 16]
```
