0

How can I create a variable that will contain for example 24 bytes of zeros in lua? I thought about something like: local zeros = "0X0000..00" (with 24 zeros).

And in addition, how can I create a variable that will be in size of 8 bytes?

  • 0x00...000 with 24 zeros is not 24 bytes. in hexadecimal notation you need two digits for one byte. so 0x000000000000000000000000 is actually 12 bytes. – Piglet Nov 25 '22 at 07:49
  • 1
    Lua uses bytestring. The zero byte can be obtained using the `\0` decimal escape. Thus `string.rep('\0', 24)` gives you a byte string with 24 zero bytes. – Luatic Nov 25 '22 at 08:18
  • @LMD so in this case: `local str = "000"` the size of this variable is 3 bytes ? – user20007266 Nov 27 '22 at 11:47
  • @user20007266 No, the size of the string contents is three bytes. The variable will require more space (it will also have to keep track of string length etc.). – Luatic Nov 27 '22 at 12:19
  • @LMD Are you sure about that? I ask because in `c language` (for example) string that contains 3 letters is not in size of 3 bytes. namely, it is not necessarily that each letter is one byte... where can I read more information about that? – user20007266 Nov 27 '22 at 14:02
  • A string that contains three Lua "characters" is a string containing three bytes (what patterns & all string lib funcs operate on are bytes). This does not mean that such a string will appear three bytes long in your editor; that depends on the encoding of your sources. If you use UTF-8, all ASCII characters, including the ASCII `0`, will be exactly one byte. Non-ASCII Unicode characters will be represented using multiple bytes. If you use UTF-16 (not recommended as Lua provides UTF-8 support), ASCII characters will be represented using two bytes. Lua strings are not null-terminated. – Luatic Nov 27 '22 at 14:46
  • [Refer to the reference manual](https://www.lua.org/manual/5.4/manual.html#6.4) for more. – Luatic Nov 27 '22 at 14:46
  • @LMD I see there: `string.rep (s, n [, sep]) Returns a string that is the concatenation of n copies of the string s separated by the string sep. The default value for sep is the empty string (that is, no separator). Returns the empty string if n is not positive.` but it's not say that it's in size of n bytes.. – user20007266 Nov 27 '22 at 17:09
  • Yes, because the purpose of Lua isn't to allow you fine-grained control over the exact memory consumption in bytes. Lua is a higher level language. – Luatic Nov 27 '22 at 18:39
  • @LMD So if for example i want to calculate SHA256 about string , and i want to get it as 32 bytes, how can i get it? I see 64 digits that in size of 64 bytes .. – user20007266 Nov 27 '22 at 19:02
  • That depends. Your SHA256 library may choose to either return a bytestring of length 32 or a hexstring of length 64. – Luatic Nov 28 '22 at 09:03

1 Answers1

0

All strings have string library functions attached as methods.
Therefore theres a simpel Answer...

$ lua
Lua 5.4.4  Copyright (C) 1994-2022 Lua.org, PUC-Rio
> Z24 = ('0'):rep(24)
> Z8 = ('0'):rep(8)
> #Z24
24
> #Z8
8
> Z24
000000000000000000000000
> Z8
00000000
> Z24 + Z8
0
> Z24 .. Z8
00000000000000000000000000000000
>
koyaanisqatsi
  • 2,585
  • 2
  • 8
  • 15