I’m trying to convert some python code to Lua. What is the Lua equivalent to:
value2 = ''
key = 'cmpg'
value1 = '\x00\x00\x00\x00\x00\x00\x00\x01'
Value2 += '%s%s%s' % (key, struct.pack('>i', len(value1)), value1)
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You said in a comment:
The effector ‘>i’ means bigendian signed 32-bit integer. For non-negative input x, the simple Python equivalent would be
chr((x >> 24) & 255) + chr((x >> 16) & 255) + chr((x >> 8) & 255) + chr(x & 255)You should be able to express that in Lua without much difficulty.
You said in yet another comment:
chr(x) is easy to find in the docs. Lua should have such a function, perhaps even with the same name.
i >> nshifts i right by n bits. If i is unsigned, this is equivalent toi // ( 2 ** n)where//is Python’s integer floor division.i & 255is a bitwise-and which is equivalent toi % 256.Lua should have both of those.
The
+is in this case string concatenation.Have a look at this:
You’ll notice that the required output for
10is'\x00\x00\x00\n'… note that'\x0a'aka'\n'akachr(10)needs care. If you are writing this stuff to a file on Windows, you must open the file in binary mode ('wb', not'w') otherwise the run-time library will insert a carriage-return byte to conform with Windows,MS-DOS,CP/M conventions for text files.