Personal tools
You are here: Home Projects SETL SETL2 Source code DB_byteutil.stl
Document Actions

DB_byteutil.stl

by Paul McJones last modified 2021-02-25 11:31

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;
« November 2024 »
Su Mo Tu We Th Fr Sa
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
 

Powered by Plone CMS, the Open Source Content Management System

This site conforms to the following standards: