Converting HEX to ASCII in Lua Dissector

10,015

Solution 1

I'm not sure what you mean by hex bytes, but the relevant functions are:

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.

Share:
10,015

Related videos on Youtube

user3688020
Author by

user3688020

Updated on June 04, 2022

Comments

  • user3688020
    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
      dualed over 9 years
      if you mean to convert a string containing hexadecimal numbers, you can use tonumber like in print(string.char(tonumber("56", 16))) (prints V)
  • user3688020
    user3688020 over 9 years
    I mean a pair of HEX bits, don't know why I decided that was a byte, thanks I'll give those functions a try.