Converting HEX to ASCII in Lua Dissector
Solution 1
I'm not sure what you mean by hex bytes, but the relevant functions are:
string.byte
, which converts chars to numerical codesstring.char
, which converts numerical codes to chars
Solution 2
For a single character in hexadecimal, you can use string.byte
as mentioned by lhf. For longer sequences, you can create a loop in Lua, but that is not very efficient since it involves a lot of copying.
Since Wireshark 1.11.3 there is a Struct.fromhex
function that converts a string of hexadecimal characters to the binary equivalent.
Example:
-- From hex to bytes (with no separators)
assert(Struct.fromhex("5753") == "WS")
-- From hex to bytes (using a single space as separator)
assert(Struct.fromhex("57 53", " ") == "WS")
Similarly, there is a Struct.tohex
function that converts from bytes to hex.
Related videos on Youtube
user3688020
Updated on June 04, 2022Comments
-
user3688020 over 1 year
I'm trying to take HEX bytes and display them as their ASCII values. If someone could point me reasonably firmly in the right direction I'd be obliged. Tried any number of uint-type commands, and working with buffer(x, 2) as an argument.
-
dualed over 9 yearsif you mean to convert a string containing hexadecimal numbers, you can use
tonumber
like inprint(string.char(tonumber("56", 16)))
(printsV
)
-
-
user3688020 over 9 yearsI mean a pair of HEX bits, don't know why I decided that was a byte, thanks I'll give those functions a try.