Skip to content

encode

Module Export

[byte] encode base64 |> text
[byte] encode base64Url |> text
[byte] encode hex |> text
[byte] encode utf8 |> text
[byte] encode text |> text

Encodes binary data to a text format using keywords like base64, base64Url, hex, or utf8/text.

The encoding type is specified using a keyword after encode.

Encoding Types

KeywordInputOutputDescription
base64[byte]textStandard Base64 encoding
base64Url[byte]textURL-safe Base64 (no padding)
hex[byte]textLowercase hexadecimal
utf8[byte]textInterpret bytes as UTF-8 text
text[byte]textAlias for utf8

Examples

Encode bytes to Base64

import { encode } from 'text'
from [72, 101, 108, 108, 111] encode base64
// Returns: 'SGVsbG8='

Encode bytes to hex

import { encode } from 'text'
from [72, 101, 108, 108, 111] encode hex
// Returns: '48656c6c6f'

Encode bytes as UTF-8 text

import { encode } from 'text'
from [72, 101, 108, 108, 111] encode utf8
// Returns: 'Hello'

Encode bytes using the text alias

import { encode } from 'text'
from [72, 101, 108, 108, 111] encode text
// Returns: 'Hello'

See Also

  • decode for decoding text to bytes