Board index » delphi » Decimal to Hex Conversion

Decimal to Hex Conversion

I am trying to develop a program that needs to convert some 8 digit numbers to their
hexadecimal equivalent. Can anyone help with some code or algorithm to complete this task?
thanks in advance.

 

Re:Decimal to Hex Conversion


On 16 Jun 1996 04:19:20 GMT, you wrote:

Quote
>I am trying to develop a program that needs to convert some 8 digit numbers to their
>hexadecimal equivalent. Can anyone help with some code or algorithm to complete this task?
>thanks in advance.

I recommend you think about it....how would you do it on paper?

89923210(base10) to base 16....

89923210 / 16 = 5620200 remainder 10
5620200 / 16 = 351262 remainder 8
351262 / 16 = 21953 remainder 14
21953 / 16 = 1372 remainder 1
1372 / 16 = 85 remainder 12
85 / 16 = 5 remainder 5
5/16 = 0 remainder 5.

89923210(base10) -=> 55C1E8A(base16)

Double-check:

5*16^6+5*16^5+12*16^4+1*16^3+14*16^2+8*16^1+10*16^0 = 89923210

Get the idea?

Also, as a suggestion, if you can bit shift to get the results (16 =
2^4), do it, as it's quicker than a MUL or DIV op code from the CPU.

Re:Decimal to Hex Conversion


Quote
Brian <Br...@geographe.com.au> wrote:
>I am trying to develop a program that needs to convert some 8 digit numbers to their
>hexadecimal equivalent. Can anyone help with some code or algorithm to complete this task?
>thanks in advance.

If you take a look at the online help on the CSeg ( or another Seg )
function (TP7) you will find a procedure called WriteHexWord. It
writes, given a word, a Hex string. I think you can easily adjust this
to your own needs.

Hope this helps.

Arjan Kleene
--
University of Twente
the Netherlands, Europe
email: a.kle...@student.utwente.nl
   or: kle...@cs.utwente.nl

Re:Decimal to Hex Conversion


In article <4q0208$...@swing.iinet.net.au> of Sun, 16 Jun 1996 04:19:20
in comp.lang.pascal.borland, Brian <Br...@geographe.com.au> wrote:

Quote
>I am trying to develop a program that needs to convert some 8 digit numbers to
>their
>hexadecimal equivalent. Can anyone help with some code or algorithm to complete
>this task?
>thanks in advance.

Here's one way, assuming that the numbers are positive and less than
2^31, and can be read in to longints - tested, but not a lot.

{$IFDEF WINDOWS} uses WinCrt ; {$ENDIF}

function Hex(X : longint) : string ;
const Dig : array [$0..$F] of char = '0123456789ABCDEF' ;
begin
  if X<=0 then Hex := '$' else Hex := Hex(X div $10) + Dig[X mod $10] ;
  end ;

var LI : longint ;

begin repeat Readln(LI) ; Writeln(LI:10, Hex(LI):12) until false end.

Efficiency was disregarded.
--
John Stockton, Surrey, UK.  J...@merlyn.demon.co.uk  Turnpike v1.12  MIME
    Home Page under construction.

Re:Decimal to Hex Conversion


B> I am trying to develop a program that needs to convert some 8 digit numbers
B> their
B> hexadecimal equivalent. Can anyone help with some code or algorithm to
B> complete this task?
B> thanks in advance.

Yes.. here is program I made for that purpose.. I found the hex converter
stuff in swag

program hex_conv;

var
  gwar:string;

function Hex(v: Longint; w: Integer): String;
var
  s               : String;
  i               : Integer;
const
  hexc            : array [0 .. 15] of Char= '0123456789abcdef';
begin
  s[0] := Chr(w);
  for i := w downto 1 do begin
    s[i] := hexc[v and $F];
    v := v shr 4
  end;
  Hex := s;
end;

begin
  gwar:=hex(77,4);
  writeln(gwar);
  gwar:=hex(4096,4);
  writeln(gwar);
end.

Re:Decimal to Hex Conversion


Quote
Brian <Br...@geographe.com.au> wrote:

+-> I am trying to develop a program that needs to convert some 8
digit numbers to their
+-> hexadecimal equivalent. Can anyone help with some code or
algorithm to complete this task?
+-> thanks in advance.

I hope the following unit helps:

======================================================================================

UNIT Hex; {FINAL}
{$A-,B-,D-,E-,F-,G-,I-,L-,N-,O-,P-,Q-,R-,S-,T-,V-,X-}

(*
    DEBIDOES (tm) Version 1.10

    Author:  Erikc Sadler.

    Copyright (C) 1993 Erikc Sadler.  ALL RIGHTS RESERVED.

    Function(s):  Converts byte, word and longint values to
hexidecimal text
        strings.
*)

INTERFACE {104/0}

{uses nothing}

FUNCTION HexByte( arg: BYTE): STRING;
FUNCTION HexWord( arg: WORD): STRING;
FUNCTION HexLong( arg: LONGINT): STRING;

IMPLEMENTATION

(*
    Putting the translation table in a 'procedure' forces it to reside
in
    the code space rather than cluttering up the global variable
space.
*)
PROCEDURE hexChars; ASSEMBLER;  {translation table}
ASM
        DB      '0123456789ABCDEF'
END; {hexChars}

PROCEDURE xDigit; ASSEMBLER;    {service to 'print' a hexdigit}
ASM
        CLD                     {string goes left-to-right}
        STOSB                   {save length of string}
        MOV     SI,OFFSET hexChars   {base of hex table}
@outer:
        MOV     BX,$400         {BH = shift count, BL = 0}
@inner:
        RCL     CX,1
        RCL     DX,1
        RCL     BL,1            {shift BL <- DX <- CX}
        DEC     BH              {and count shifts}
        JNE     @inner          {until BH = 0}
        MOV     AL,CS:[SI+BX]   {get hex character}
        STOSB                   {and save it}
        DEC     AH              {decrement digit count}
        JNE     @outer
END; {xDigit}

(*
    Convert a byte to a 2-character hex string.
*)
FUNCTION HexByte( arg: BYTE): STRING; ASSEMBLER;  {113}
ASM
        LES     DI,@result
        MOV     DH,arg
        MOV     AX,$202         {length of byte string in AH, AL}
        CALL    xDigit
END; {HexByte}

(*
    Convert a word to a 4-character hex string.
*)
FUNCTION HexWord( arg: WORD): STRING; ASSEMBLER;  {115}
ASM
        LES     DI,@result
        MOV     DX,arg
        MOV     AX,$404         {length of byte string in AH, AL}
        CALL    xDigit
END; {HexWord}

(*
    Convert a longint to a 8-character hex string.
*)
FUNCTION HexLong( arg: LONGINT): STRING; ASSEMBLER;
ASM
        LES     DI,@result
        MOV     DX,WORD[ arg].2
        MOV     CX,WORD[ arg].0
        MOV     AX,$808         {length of byte string in AH, AL}
        CALL    xDigit
END; {HexLong}

END.

Erikc

Anything critical said of me by a religious zealot only
further convinces me that I am on the right path to truth,
inner peace, enlightenment, and other such delights. -- Me

Other Threads