Standard Library Hex Functions

function

encode_hex

encode_hex encodes a buffer or string as a hexadecimal string with 0x prefix.

Inputs

  • Name
    value
    Required
    required
    Type
    buffer | string | addon
    Description

    The buffer or string to encode. Strings starting with '0x' are decoded as hex; otherwise, raw UTF-8 bytes are used.

Output

  • Name
    value
    Type
    string
    Description

    The input in its hexadecimal representation with 0x prefix.

Example using encode_hex

output "encoded_hex" {
    value = encode_hex("hello, world")
}
// > encoded_hex: 0x68656c6c6f2c20776f726c64


function

decode_hex

decode_hex decodes a hexadecimal string and returns the result as a buffer.

Inputs

  • Name
    hex_string
    Required
    required
    Type
    string
    Description

    The hex string to decode (with or without 0x prefix).

Output

  • Name
    value
    Type
    buffer
    Description

    The decoded hex string as a buffer.

Example using decode_hex

output "decoded_hex" {
    value = decode_hex("0x68656c6c6f2c20776f726c64")
}
// > decoded_hex: 0x68656c6c6f2c20776f726c64


Was this page helpful?