Expand/Shrink

bytes_to_int

Definition: atom a = bytes_to_int(sequence s, bool signed=true)
Description: Convert a sequence of byte values to an atom.
The elements of s should be in the order expected, i.e. little-endian/least-significant byte first on an x86-based machine.
pwa/p2js: Supported.
Comments: The result could be greater than the integer type allows, so you should assign it to an atom.

s would normally contain positive values that have been read using peek() from 1, 2, 4, or 8 consecutive memory locations.
By default the result will be a signed integer, honouring the sign bit, but you can treat it as unsigned by passing false (0) as the second parameter.
This routine can be used for byte/word/dword/qword conversion, and is just a simple wrapper for peekNS().
An error occurs if s contains any non-atom elements, or the length is not one of 1, 2, 4, or 8 (unlike Euphoria, which pads/trims it to length 4).
Better performance may be achived by using peekNS() (or any other variant of peek) directly, unless s is a slice of a much larger peek, or manually constructed, etc.
Example:
atom int32
int32 = bytes_to_int({37,1,0,0})
-- int32 is 37 + #100*1 = 293
Implementation: See builtins\machine.e (an autoinclude) for details of the actual implementation.
See Also: int_to_bytes, bits_to_int, peek, poke