Re:Integer to HEX conversion ???
Here try this
-----------------------------------------------------------------
FUNCTION hexstr(VAR v; len : BYTE) : STRING;
CONST hexchars : ARRAY[0..15] OF CHAR = ('0','1','2','3','4','5','6',
'7','8','9','A','B','C','D','E','F');
VAR
c : ARRAY[1..4] OF BYTE ABSOLUTE V;
s : STRING[255];
i : BYTE;
BEGIN
s := '';
FOR i := len DOWNTO 1 DO
BEGIN
s := s + hexchars[c[i] SHR 4];
s := s + hexchars[c[i] AND $0F];
END;
hexstr := s;
END;
------------------------------------------------------
Notice "v" is an untyped parameter so the function needs to know the sizeof
the parameter. It works for all the "common" positive "whole number" values,
e.g. byte, word, integer, longint etc. Don't pass it something like a record
etc. as the compiler can't trap that mistake using untyped parameters.
Notice also that there's no check for "len <= 4", if you want to use it for
some "cardinal" type larger than 4 bytes (32 bits) change the "c :
ARRAY[1..4]" as appropriate (might also require you to fiddle with bytes
depending on the high/low word/byte order it's stored in memory as).
Call it like this
var
a : byte;
b : integer;
c : longint;
begin
writeln(hexstr(a,sizeof(a)));
writeln(hexstr(b,sizeof(b)));
writeln(hexstr(c,sizeof(c)));
Hope this helps
Paul J. Poirier (pjp)
p...@istar.ca