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('一些数据'))
-- "e4b880e4ba9be695b0e68dae"
--
-- Example 2:
sys.alert((string.to_hex('一些数据'):gsub('(..)', '\\x%1')))
-- "\xe4\xb8\x80\xe4\xba\x9b\xe6\x95\xb0\xe6\x8d\xae"

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

More examples

At the end of this chapter