$float32: Difference between revisions

From Scriptwiki
Jump to navigation Jump to search
Aca20031 (talk | contribs)
mNo edit summary
 
Aca20031 (talk | contribs)
m uses
 
Line 4: Line 4:


All credits go to [[User:Saturn|Saturn]]
All credits go to [[User:Saturn|Saturn]]
==Common Uses==
Scenarios in which would be dealing with four bytes instead of the float itself are when you would be reading low level data. Typically, this is done when reading from database structured files or TCP/UDP packets.
The most common scenario is the time_played field of the query packets for source servers (Such as Counter-Strike source).


==Script==
==Script==

Latest revision as of 22:22, 1 October 2008

Converts four bytes to a 32-bit float

$float32(byte1, byte2, byte3, byte4)

All credits go to Saturn

Common Uses

Scenarios in which would be dealing with four bytes instead of the float itself are when you would be reading low level data. Typically, this is done when reading from database structured files or TCP/UDP packets. The most common scenario is the time_played field of the query packets for source servers (Such as Counter-Strike source).

Script

alias float32 {
  var %d = $calc($1 + $2 * 256 + $3 * 256^2 + $4 * 256^3)
  var %e = $calc($and(%d,$calc(2^31-2^23))/(2^23))
  var %m = $base(1. $+ $right($base(%d,10,2,23),23),2,10)
  return $calc(-1^$isbit(%d,32) * %m * 2^(%e - 127))
}