an absolute beginner : HEX question
Author Message
an absolute beginner : HEX question

Quote:
> Could anybody give me some help converting positive dec integers to hex?

> Or woul anybody send me a sample coding for converting positive dec
> integers to hex?

> I have been puzzling over his for hours and I must admit I feel very
> stupid.

> This is not a project or anything !

Here is some code (which I probably found in this NG):
{ convert decimal to Hex value in Pascal }
type WordRecord = record
lo,hi : Byte
end;
type LongintRecord = record
case boolean of
True : (lo,hi : word);
False: (LoLo,LoHi,HiLo,HiHi : Byte)
end;
Function Hex(Arg : Byte): string;
const HexDigit: array[0..15] of char = '0123456789ABCDEF';
begin
Hex := HexDigit[Arg Shr  4]+HexDigit[Arg and 15]
end;

Function  HexWord(Num : Word): string;
begin
with WordRecord(Num) do HexWord := Hex(hi)+Hex(lo)
end;

function HexLong(L : LongInt): String;
begin
with LongIntRecord(L) do
HexLong := Hex(HiHi)+Hex(HiLo)+Hex(LoHi)+Hex(LoLo)
end;
begin
end.

Wed, 18 Jun 1902 08:00:00 GMT
an absolute beginner : HEX question

Could anybody give me some help converting positive dec integers to hex?

Or woul anybody send me a sample coding for converting positive dec
integers to hex?

I have been puzzling over his for hours and I must admit I feel very
stupid.

This is not a project or anything !

Thanks,

MARK

Wed, 18 Jun 1902 08:00:00 GMT
an absolute beginner : HEX question

Quote:
> Could anybody give me some help converting positive dec integers to hex?

> Or woul anybody send me a sample coding for converting positive dec
> integers to hex?

> I have been puzzling over his for hours and I must admit I feel very
> stupid.

> This is not a project or anything !

> Thanks,

> MARK

One way:
- convert the least-significant nybble into a hex digit by means of a
look-up table
- get the next digit by shifting right 4 bits
- loop until the entire decimal value is done.
Repeat loop is more appropriate than While here so you don't have to handle
'0' as a special case.

Example:
Function Hex(dec:LongInt; Lgt:Integer):String;
Const
HexDigits : Array[0..15] of Char = '0123456789ABCDEF';
Var
Result : String;
begin
Result := '';
Repeat
Result := HexDigits[dec and \$F]+Result;
Dec := Dec shr 4;
until (Dec=0) and (Length(Result) >= Lgt);
Hex := Result;
End;

which also allows you to specify a minimum length, I usually want my hex as
e.g. Hex(255,0) is FF

Dec is treated as unsigned. In other words, if you ask for Hex(-1,0) you
get FFFFFFFF which is the binary representation of -1 if signed and would
be  4294967295 if LongInts were unsigned.

Frank

Wed, 18 Jun 1902 08:00:00 GMT
an absolute beginner : HEX question

Quote:
> Could anybody give me some help converting positive dec integers to hex?
> Or woul anybody send me a sample coding for converting positive dec
> integers to hex?

tsfaqp.zip Common Turbo Pascal Questions and Timo's answers

42. *****
Q: How do I convert a decimal word into a hexadecimal string, etc?

A: Here is one possibility
function HEXFN (decimal : word) : string;
const hexDigit : array [0..15] of char = '0123456789ABCDEF';
begin
hexfn := hexDigit[(decimal shr 12)]
+ hexDigit[(decimal shr 8) and \$0F]
+ hexDigit[(decimal shr 4) and \$0F]
+ hexDigit[(decimal and \$0F)];
end;  (* hexfn *)
Here is another conversion example (from longint to binary string)
function LBINFN (decimal : longint) : string;
const BinDigit : array [0..1] of char = '01';
var i     : byte;
binar : string;
begin
FillChar (binar, SizeOf(binar), ' ');
binar[0] := chr(32);
for i := 0 to 31 do
binar[32-i] := BinDigit[(decimal shr i) and 1];
lbinfn := binar;
end;  (* lbinfn *)
For a full set of conversions, both from and to decimal, apply
TSUTNTB.TPU from ftp://garbo.uwasa.fi/pc/ts/tspa3570.zip.
--------------------------------------------------------------------

All the best, Timo

....................................................................

Moderating at ftp:// & http://garbo.uwasa.fi/ archives 193.166.120.5
Department of Accounting and Business Finance  ; University of Vaasa

Spam foiling in effect.  My email filter autoresponder will return a
required email password to users not yet in the privileges database.

Wed, 18 Jun 1902 08:00:00 GMT
an absolute beginner : HEX question

You could use this:
FUNCTION XBPriHex(L:Byte;B:LongInt):String;
ASSEMBLER;
ASM
STD

XOR  BH,BH
MOV  BL,L
MOV  ES:[DI],BL
DB   \$66

PUSH AX
AND  AL,\$0F
CMP  AL,\$3A

POP  AX
DB   \$66
SHR  AX,4
DEC  BL

END;

Quote:

>Could anybody give me some help converting positive dec integers to hex?
>Or woul anybody send me a sample coding for converting positive dec
>integers to hex?
>I have been puzzling over his for hours and I must admit I feel very
>stupid.
>This is not a project or anything !
>Thanks,
>MARK

Wed, 18 Jun 1902 08:00:00 GMT

 Page 1 of 1 [ 5 post ]

Relevant Pages