DB_byteutil.stl
Byte string and integer conversion functions.
package byteutil; procedure int_of_4(stg); -- convert a 4-byte string to an integer procedure stg_of_4(the_int); -- convert an integer to a 4-byte string procedure int_of_5(stg); -- convert a 4-byte string to an integer procedure stg_of_5(the_int); -- convert an integer to a 4-byte string procedure int_from_bytes(stg); -- convert two byte string into integer procedure bytes_from_int(int); -- convert integer into two byte string procedure int_from_byte(stg); -- convert one byte string into integer procedure byte_from_int(int); -- convert integer into one byte string end; package body byteutil; procedure int_of_4(stg); -- convert a 4-byte string to an integer return abs(stg(4)) + 256 * (abs(stg(3)) + 256 * (abs(stg(2)) + 256 * (abs(stg(1))))); end int_of_4; procedure stg_of_4(the_int); -- convert an integer to a 4-byte string stg := ""; -- will collect for j in [1..4] loop stg := char(the_int mod 256) + stg; the_int /:= 256; end loop; return stg; end stg_of_4; procedure int_of_5(stg); -- convert a 5-byte string to an integer return abs(stg(5)) + 256 * (abs(stg(4)) + 256 * (abs(stg(3)) + 256 * (abs(stg(2)) + 256 * abs(stg(1))))); end int_of_5; procedure stg_of_5(the_int); -- convert an integer to a 5-byte string stg := ""; -- will collect for j in [1..5] loop stg := char(the_int mod 256) + stg; the_int /:= 256; end loop; return stg; end stg_of_5; procedure int_from_bytes(stg); -- convert two byte string into integer return abs(stg(1)) * 256 + abs(stg(2)); end int_from_bytes; procedure bytes_from_int(int); -- convert integer into two byte string return char(int/256) + char(int mod 256); end bytes_from_int; procedure int_from_byte(stg); -- convert one byte string into integer return abs(stg); end int_from_byte; procedure byte_from_int(int); -- convert integer into one byte string return char(int); end byte_from_int; end;