Board index » delphi » Integer to HEX conversion ???

Integer to HEX conversion ???

Hi,
I wanna know if there is a built in function that
can convert an Integer value into a string representing it's
value in Hexadecimal notation.
(I'm using BP 7.0)

Thanks everyone !!!!

Regards from MIODRAG

 

Re:Integer to HEX conversion ???


In article <6nqvrm$30...@SOLAIR2.EUnet.yu>,
Vladimir & Miodrag Perunicic <vlad...@eunet.yu> wrote:
:I wanna know if there is a built in function that
:can convert an Integer value into a string representing it's
:value in Hexadecimal notation.

No, but constants can be decimal or hexadecimal ($). For the
conversion:

 143222 May 26 1998 ftp://garbo.uwasa.fi/pc/link/tsfaqp.zip
 tsfaqp.zip Common Turbo Pascal Questions and Timo's answers, linked

   All the best, Timo

....................................................................
Prof. Timo Salmi   Co-moderator of news:comp.archives.msdos.announce
Moderating at ftp:// & http://garbo.uwasa.fi/ archives 193.166.120.5
Department of Accounting and Business Finance  ; University of Vaasa
mailto:t...@uwasa.fi <http://www.uwasa.fi/~ts/>  ; FIN-65101,  Finland

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

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

Re:Integer to HEX conversion ???


Vladimir & Miodrag Perunicic wrote:

Quote
> Hi,
> I wanna know if there is a built in function that
> can convert an Integer value into a string representing it's
> value in Hexadecimal notation.
> (I'm using BP 7.0)

> Thanks everyone !!!!

> Regards from MIODRAG

Try this on for size...

Type  TinyStr=String[20];

     Function Num2HexStr (Num: LongInt; Len: Byte): TinyStr;
          Var   IsNegative: Boolean;
                C,Nibble: Byte;
                QC: String[10];
     Begin
          If Len<127 Then QC:='0000000000'
                     Else QC:='          ';
          Len:=(Len And 127);
          IsNegative:=False;
          If Num<0 Then
               Begin
                    IsNegative:=True;
                    Num:=Num*-1;
               End;
          C:=10;
          While Num>0 Do
          Begin
               Nibble:=Num And $0000000F;
               Case Nibble Of
                    0..9: QC[C]:=Chr(48+Nibble);
                    Else  QC[C]:=Chr(55+Nibble);
               End;
               Num:=Num Div 16;
               Dec(C,1);
               If C<1 Then Break;
          End;
          If C>0 Then
               Begin
                    Delete(QC,C,1);
                    If IsNegative Then Insert('-',QC,C)
                                  Else Insert('$',QC,C);
               End;
          While (Length(QC)>Len) And (Length(QC)>1) Do
               Delete(QC,1,1);
          Num2HexStr:=QC;
     End;

Other Threads