Difference between revisions of "Lua:stringToByteTable"
m (Syntax Highlighting.) |
|||
| (7 intermediate revisions by 3 users not shown) | |||
| Line 1: | Line 1: | ||
[[Category:Lua]] | [[Category:Lua]] | ||
| − | '''function''' stringToByteTable(''String'') ''':''' | + | {{CodeBox|'''function''' stringToByteTable(''String'') ''':''' Table}} |
| − | Converts a string to a byte | + | Converts a string to a table of bytes, where each byte represents the ASCII/ANSI value of the corresponding character. |
| − | + | ===Function Parameters=== | |
| − | === Function Parameters === | + | {|width="85%" cellpadding="10%" cellspacing="0" border="0" |
| − | {|width="85%" cellpadding="10 | ||
!align="left"|Parameter | !align="left"|Parameter | ||
!align="left"|Type | !align="left"|Type | ||
| Line 12: | Line 11: | ||
|- | |- | ||
|String | |String | ||
| − | | | + | |String |
| − | |The string to convert | + | |The string to convert. |
|} | |} | ||
| + | ===Returns=== | ||
| + | Table — A table containing the bytes representing each character in the string. | ||
| + | |||
| + | ===Explanation=== | ||
| + | |||
| + | The "ToByteTable" functions convert a value (such as an integer or floating-point number) into a table of bytes that represent how the value is stored in memory. | ||
| + | |||
| + | For example, if you use <code>wordToByteTable(1337)</code>: | ||
| − | + | - The decimal number <code>1337</code> is <code>0x0539</code> in hexadecimal. | |
| − | + | - Computers typically use little-endian order, meaning the least significant byte comes first. | |
| − | + | - So, <code>0x0539</code> is split into two bytes: <code>0x39</code> (57 in decimal) and <code>0x05</code> (5 in decimal). | |
| − | + | - The resulting byte table is <code>{0x39, 0x05}</code> or <code>{57, 5}</code>. | |
| + | This means: | ||
| + | - <code>bytes[1] = 0x39</code> (the low byte) | ||
| + | - <code>bytes[2] = 0x05</code> (the high byte) | ||
| − | + | This process applies to all "ToByteTable" functions: | |
| − | + | They break down the value into its raw bytes, ordered from least significant to most significant (little-endian). | |
| − | |||
| − | |||
| − | |||
| − | + | ===Examples=== | |
| − | + | <syntaxhighlight lang="lua" line> | |
| − | + | local bytes = stringToByteTable("ABC") | |
| − | + | for i, b in ipairs(bytes) do | |
| − | + | print(string.format("Byte %d: %02X", i, b)) | |
| + | end | ||
| + | -- Output: Byte 1: 41, Byte 2: 42, Byte 3: 43 (hex for 'A', 'B', 'C') | ||
| + | </syntaxhighlight> | ||
| + | {{LuaSeeAlso}} | ||
| − | + | {{RelatedStrings}} | |
| − | |||
| − | |||
| − | + | {{ReadWriteMemory}} | |
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
Latest revision as of 15:47, 25 June 2026
Converts a string to a table of bytes, where each byte represents the ASCII/ANSI value of the corresponding character.
Function Parameters[edit]
| Parameter | Type | Description |
|---|---|---|
| String | String | The string to convert. |
Returns[edit]
Table — A table containing the bytes representing each character in the string.
Explanation[edit]
The "ToByteTable" functions convert a value (such as an integer or floating-point number) into a table of bytes that represent how the value is stored in memory.
For example, if you use wordToByteTable(1337):
- The decimal number 1337 is 0x0539 in hexadecimal.
- Computers typically use little-endian order, meaning the least significant byte comes first.
- So, 0x0539 is split into two bytes: 0x39 (57 in decimal) and 0x05 (5 in decimal).
- The resulting byte table is {0x39, 0x05} or {57, 5}.
This means:
- bytes[1] = 0x39 (the low byte)
- bytes[2] = 0x05 (the high byte)
This process applies to all "ToByteTable" functions: They break down the value into its raw bytes, ordered from least significant to most significant (little-endian).
Examples[edit]
1 local bytes = stringToByteTable("ABC")
2 for i, b in ipairs(bytes) do
3 print(string.format("Byte %d: %02X", i, b))
4 end
5 -- Output: Byte 1: 41, Byte 2: 42, Byte 3: 43 (hex for 'A', 'B', 'C')