Skip to main content

Convert to hex string (string.to_hex)

Declaration​

hex_text = string.to_hex(data[, prefix, suffix])

Parameters​

  • data
    string. The string (or binary data) to convert to hex text.
  • prefix
    string, optional. Prefix for each converted byte. Default: "".
  • suffix
    string, optional. Suffix for each converted byte. Default: "".

Returns​

  • hex_text
    string. The hex-encoded text.

Description​

Convert a string (or binary data) to printable hex text.
Keywords: image hex, hex image, file hex, hex file, data hex, hex data

Example​

-- Example 1:
sys.alert(string.to_hex('some data'))
-- "736f6d652064617461"
--
-- Example 2:
sys.alert((string.to_hex('some data'):gsub('(..)', '\\x%1')))
-- "\x73\x6f\x6d\x65\x20\x64\x61\x74\x61"

Note: Uses function outside of this chapter: sys.alert

More examples​

At the end of this chapter