Board index » delphi » HELP. Need a procedure to change decimal to hex.

HELP. Need a procedure to change decimal to hex.

Can anyone help?

Thanks

John Albert
jalb...@pixi.com

 

Re:HELP. Need a procedure to change decimal to hex.


John F. Albert (jalb...@pixi.com) wrote:
: Can anyone help?

: Thanks

: John Albert
: jalb...@pixi.com

This should work :

function intenHex(w: WORD) : string;
CONST
  hexChars: ARRAY [0..$F] OF CHAR =
    '0123456789ABCDEF';
var s : string;
BEGIN
     s:=hexChars[Hi(w) SHR 4]+hexChars[Hi(w) AND $F]+hexChars[Lo(w) SHR 4]+hexChars[Lo(w) AND $F]+'h';
     intenhex:=s;
END;

Hope this will help,
                                F.

Fabrice.Pre...@linux.univ-compiegne.fr
WWW : Http://linux.univ-compiegne.fr/~premel/index.html

Re:HELP. Need a procedure to change decimal to hex.


Quote
d...@sierra.net (Denis Renard) wrote:
>In article <489ofk...@rigel.pixi.com>,
>   jalb...@pixi.com (John F. Albert) wrote:
>>Can anyone help?

>>Thanks

>>John Albert
>>jalb...@pixi.com

>If you're using Delphi, try IntToHex.
>Otherwise you can try this code:
>function convertToHex(i : word) : string;
>{ convert an integer into an hexadecimal string
>  -- word is used instead of int to handle negative numbers --
>  In Delphi use IntToHex(Value: Longint; Digits: Integer): string;
>}
>const
>    hexchar : string = '0123456789ABCDEF';
>var
>   sbuf : string[4];   { 4 = 16 BITS = a word }
>begin
>   sbuf := '';
>   if i=0 then sbuf:='0' else
>   while i<>0 do begin
>     sbuf := hexChar[i and 15+1]+sbuf;  { i and 15 = i modulo 16 }
>     i := i shr 4;                      { i shr 4 = i div 16 }
>   end;
>   convertToHex := sbuf;
>end;
>Maybe there is an equivalent for IntToHex in Borland Pascal...
>Hope this helps, Denis

There isn't an equivalent in Borland Pascal.  In basic it is Hex$(x)
or some such.  But, I made a function and procedure to do it last
night.  

Thanks anyway,

John

Re:HELP. Need a procedure to change decimal to hex.


This is an other way...

function HexW(W : Word) : string;
const Digits : array[0..$F] of Char = '0123456789ABCDEF';
  begin
  HexW[0] := #4;
  HexW[1] := Digits[hi(W) shr 4];
  HexW[2] := Digits[hi(W) and $F];
  HexW[3] := Digits[lo(W) shr 4];
  HexW[4] := Digits[lo(W) and $F];
  end;

{Done!}

Re:HELP. Need a procedure to change decimal to hex.


Quote
In article <816554024.16...@mbaldwin.demon.co.uk> Mark Baldwin <m...@mbaldwin.demon.co.uk> writes:
>From: Mark Baldwin <m...@mbaldwin.demon.co.uk>
>*NOTE* i did not write this and just in case the the ppl who
>did write are reading this then plz don't hassle me, it may
>be copyrighted but lets face it its the ONLY way to do it
>properly :)

No: IMHO it's cleaner to make HexW from two HexB (as you make HexL from two
HexW).  Yours is better than the previous, but certainly not the ONLY proper
way.
--
John Stockton : mailto:J...@dclf.npl.co.uk from off-site.  MIME.  WP.
 National Physical Laboratory, Teddington, Middlesex, TW11 0LW, UK
  Direct Phone +44 181-943 6087, Nearby Fax +44 181-943 7138  
   Postings out, Email in/out are fast.  Offshore news takes 0..10+
    days to arrive; please mail me a copy of non-UK followups!  
     Regret system puts unzoned (UK civil) time on messages.

Re:HELP. Need a procedure to change decimal to hex.


On 18 Nov 95 (10:17), jalb...@pixi.com wrote:

Quote
> I'm still pretty new to programming in Pascal.  I don't know how to
> use many of the functions and reserved words and such yet.
> This is the Decimal to Hex program I made.  I know it's long and
> sloppy, but I'm still pretty green.  It topped out at 32767, but the
> use of longint would let it goto 2 billion or some such.

> John
> jalb...@pixi.com

> program HexConvt;

> uses wincrt;

> var
>      HexNumber : integer;

> function Hex(N : integer): char;
>          begin
>              if N = 15 then
>                 Hex := 'F'
>              else if N = 14 then
>                   Hex := 'E'
>              else if N = 13 then
>                   Hex := 'D'
>              else if N = 12 then
>                   Hex := 'C'
>              else if N = 11 then
>                   Hex := 'B'
>              else if N = 10 then
>                   Hex := 'A'
>              else
>                  Hex := chr(N + 48);
>              end;

> procedure HexConv(var E : integer);
>           var
>              First,
>              Second,
>              Third  : integer;
>           begin
>                writeln;
>                write('The hex equivalent is ');
>                if (E <= 32767) and (E >= 4096) then

>                   begin
>                        First := trunc(E / 4096);
>                        write (Hex(First));
>                        E := E - (First * 4096);
>                   end
>                else
>                    write ('0');
>                if (E <= 4095) and (E >= 256) then
>                   begin
>                        Second := trunc(E / 256);
>                        write (Hex(Second));
>                        E := E - (Second * 256) ;
>                   end
>                else
>                    write ('0');
>                if (E <= 255) and (E >= 16) then
>                   begin
>                        Third := trunc(E / 16);
>                        write (Hex(Third));
>                        E := E -(Third * 16);
>                   end
>                else
>                    write ('0');

>                writeln (Hex(E));
>                writeln;
>           end;
> begin
>      readln (HexNumber);
>      HexConv (HexNumber);
> end.

Try this code (it works for longints !):

type
  THexStr = string[8];

function GetHexStr(Value: longint): THexStr;
const
  HexChars: array[0..15] of char = '0123456789ABCDEF';
begin
  GetHexStr := HexChars[Value shr 28] +
               HexChars[(Value shr 24) and 15] +
               HexChars[(Value shr 20) and 15] +
               HexChars[(Value shr 16) and 15] +
               HexChars[(Value shr 12) and 15] +
               HexChars[(Value shr 8) and 15] +
               HexChars[(Value shr 4) and 15] +
               HexChars[Value and 15];
end;

var
  l: longint;

begin
  write('Enter a decimal value..... ');
  readln(l);
  writeln('The hexadecimal value is: ', GetHexStr(l));
  writeln;
end.

If you need more help, you can mail me.

Stephan

+------------------------------------------------------------------+
| Stephan A. Maciej                          step...@maciej.muc.de |
| !!! NEW !!!   http://www.muc.de/~stephanm   (under developement) |
+------------------------------------------------------------------+

Other Threads