This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

perl pack equivalent in lua
#1
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.
Reply
#2
This should do the trick:
Code:
data = "00990102"
checksum = 49
chbyte = string.format("%02X", checksum)
command = lmcore.hextostr("07F0" .. data .. chbyte .. "070F", true)
Reply
#3
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!
Reply
#4
(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
Reply


Forum Jump: