LogicMachine Forum
perl pack equivalent in lua - Printable Version

+- LogicMachine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8)
+--- Thread: perl pack equivalent in lua (/showthread.php?tid=3224)



perl pack equivalent in lua - baggins - 15.03.2021

Hi,

I am trying to convert a perl script to lua and ran into the pack command.
Simplified, this is what's in the script:

Code:
#!/usr/bin/perl -l $data = "00990102"; $checksum = 49;     my $command = pack("H*","07F0" . $data . $checksum . "070F"); print $command;

This results in :
Code:
./test.pl |od -x --endian big 0000000 07f0 0099 0102 4907 0f0a

How can I achieve this in lua?
The perl script also uses the unpack command...

Thanks for your help.


RE: perl pack equivalent in lua - admin - 15.03.2021

This should do the trick:
Code:
data = "00990102" checksum = 49 chbyte = string.format("%02X", checksum) command = lmcore.hextostr("07F0" .. data .. chbyte .. "070F", true)



RE: perl pack equivalent in lua - baggins - 15.03.2021

Thanks admin.

Now, I am developping this script not on LM but on a 'normal' Linux box, because I don't want to use the LM that is in production... Is there some standard lua equivalent for lmcore.hextostring?

Also the perl script converts the command to a readable string for logging purposes:

Code:
$commandhex =~ s/(.)/sprintf("0x%x ",ord($1))/eg;

How would that be done in lua?

Thanks!


RE: perl pack equivalent in lua - baggins - 15.03.2021

(15.03.2021, 14:21)baggins Wrote: Thanks admin.

Now, I am developping this script not on LM but on a 'normal' Linux box, because I don't want to use the LM that is in production... Is there some standard lua equivalent for lmcore.hextostring?

Also the perl script converts the command to a readable string for logging purposes:

Code:
$commandhex =~ s/(.)/sprintf("0x%x ",ord($1))/eg;

How would that be done in lua?

Thanks!

I've found a script and written another one that I can use on my Linux box.

Code:
function string.fromhex(str)     return (str:gsub('..', function (cc)         return string.char(tonumber(cc, 16))     end)) end function printhex (str)     length = string.len(str)     result = ""     for i = 1, length,2 do         result = result..'0x'..string.sub(str,i,i+1).." "     end     return result end