Difference between revisions of "Lua:floatToByteTable"

From Cheat Engine
Jump to navigation Jump to search
m
Line 1: Line 1:
 
[[Category:Lua]]
 
[[Category:Lua]]
−
'''function''' floatToByteTable(''Number'') ''':''' table
+
'''function''' floatToByteTable(''Number'') ''':''' Table
  
−
Converts a DWORD (4 bytes), interpreted as a single precision floating point, to a byte table.
+
Converts a single precision (32-bit) floating point number to a table of bytes.
  
−
 
+
===Function Parameters===
−
=== Function Parameters ===
+
{|width="85%" cellpadding="10%" cellspacing="0" border="0"
−
{|width="85%" cellpadding="10%" cellpadding="5%" cellspacing="0" border="0"
 
 
!align="left"|Parameter
 
!align="left"|Parameter
 
!align="left"|Type
 
!align="left"|Type
Line 12: Line 11:
 
|-
 
|-
 
|Number
 
|Number
−
|number
+
|Number
−
|The number to convert to a byte table
+
|The single precision floating point value to convert.
 
|}
 
|}
  
 +
===Returns===
 +
Table — A table containing the bytes representing the float.
  
−
== Examples ==
+
===Examples===
−
  local bt = floatToByteTable(100.0)
+
<pre>
−
  writeBytes('00123ABC', bt)
+
local bytes = floatToByteTable(3.14)
−
 
+
for i, b in ipairs(bytes) do
−
 
+
  print(string.format("Byte %d: %02X", i, b))
−
Code:
+
end
−
  local bt = floatToByteTable(10)
+
-- Output: 4 bytes representing the float in little-endian order
−
  for i, v in ipairs(bt) do
+
</pre>
−
    print(i - 1, string.format('%02X', v))
 
−
  end
 
−
 
 
−
Output:
 
−
  0 00
 
−
  1 00
 
−
  2 20
 
−
  3 41
 
−
 
 
−
 
 
−
Code:
 
−
  local bt = floatToByteTable(10.9)
 
−
  for i, v in ipairs(bt) do
 
−
    print(i - 1, string.format('%02X', v))
 
−
  end
 
−
 
 
−
Output:
 
−
  0 66
 
−
  1 66
 
−
  2 2E
 
−
  3 41
 
−
 
 
  
 
{{LuaSeeAlso}}
 
{{LuaSeeAlso}}

Revision as of 17:26, 11 July 2025

function floatToByteTable(Number) : Table

Converts a single precision (32-bit) floating point number to a table of bytes.

Function Parameters

Parameter Type Description
Number Number The single precision floating point value to convert.

Returns

Table — A table containing the bytes representing the float.

Examples

local bytes = floatToByteTable(3.14)
for i, b in ipairs(bytes) do
  print(string.format("Byte %d: %02X", i, b))
end
-- Output: 4 bytes representing the float in little-endian order

Main Pages

Core Lua documentation entry points

◆ Lua
◆ Script Engine

Related Functions