updating oE int_to_bytes


include convert.e 
namespace convert 
public function int_to_bytes(atom x, integer size = 4) 

converts an atom that represents an integer to a sequence of 4 bytes.

  1. x : an atom, the value to convert.

A sequence, of 4 bytes, lowest significant byte first.


If the atom does not fit into a 32-bit integer, things may still work right:

  • If there is a fractional part, the first element in the returned value will carry it. If you poke the sequence to RAM, that fraction will be discarded anyway.
  • If x is simply too big, the first three bytes will still be correct, and the 4th element will be floor(x/power(2,24)). If this is not a byte sized integer, some truncation may occur, but usually no error.

The integer can be negative. Negative byte-values will be returned, but after poking them into memory you will have the correct (two's complement) representation for the 386+.

Example 1:
s = int_to_bytes(999) 
-- s is {231, 3, 0, 0} 
Example 2:
s = int_to_bytes(-999) 
-- s is {-231, -4, -1, -1} 
See Also:

bytes_to_int, int_to_bits, atom_to_float64, poke4

Not Categorized, Please Help


Quick Links

User menu

Not signed in.

Misc Menu