Title: Convert Numbers to Hexadecimals
Question: From time to time, we ne a value displayed in hexadecimal
representation.
Answer:
The IntToStr does only decimal, so simply use this quick functions.
const HexDigs: array [0..15] of char = '0123456789ABCDEF';
function HexByte(b: Byte): string;
var bz: Byte;
begin
bz:= b and $F;
b:= b shr 4;
HexByte:= HexDigs[b] + HexDigs[bz];
end;
function HexWord(w: Word): string;
begin
HexWord := HexByte(HI(w)) + HexByte(LO(w));
end;
function HexLong(l: Longint): string;
var x: record
case Boolean of
true: (a: Longint);
false: (l, h: Word);
end;
begin
x.a := l;
HexLong:= HexByte(HI(x.h)) +
HexByte(LO(x.h)) +
HexByte(HI(x.l)) +
HexByte(LO(x.l));
end (*HexWord*);
In HexLong we use another pascal feature, called "the old case trick". In Pascal, it is possible to declare a record with a case statement. The compiler puts all variables declared in the case statement to the same adress, so in case of cases 8-) we can treat the variable as we like it. Here we put a longint and two word on each other, so we can write in the long value and read out as two words.