Sending static hexadecimal data.

Here’s another lua newbie (which will be obvious :slight_smile: )

I am trying to do a simple plugin that is based on the pioneer receiver plugin. Basically I want to send hexadecimal strings.

For example, RecallMemory00 is a static hex string:

["InputSelection1"] = { ["RecallMemory00"] = "0107000003FF0C00000E", -- Recall Memory 00

(i.e. 01 is first byte to be sent as binary 1)

… and I basically want to send it using luup.io.write() in raw binary, i.e. I somehow have to convert the complete (10char) hex string into 5 byte binary, or better, define the string as hex directly.

In C I would probably do it like this;
const char *RecallMemory00 = { ‘\x01’, ‘\x07’, ‘\x00’ … etc etc

Hope my question is clear :slight_smile:
Micael

Replying to myself; I found these function which seams to do the job in runtime;

[code]function string.fromhex(str)
return (str:gsub(‘…’, function (cc)
return string.char(tonumber(cc, 16))
end))
end

function string.tohex(str)
return (str:gsub(‘.’, function (c)
return string.format(‘%02X’, string.byte(c))
end))
end[/code]

Used like:

("Hello world!"):tohex() --> 48656C6C6F20776F726C6421 ("48656C6C6F20776F726C6421"):fromhex() --> Hello world!

Found them on stackoverflow.com.