Board index » delphi » Converting a decimal to a hex string

Converting a decimal to a hex string

How can I convert an byte to a hex string in TPW v1.5?
In TP v7 (dos) I used to use:

Function Dec2Hex (decno : integer) : string;
Var
   workinghex : string;
Begin
FormatStr(Workinghex, '%2x',decno);
Dec2Hex := workinghex;
End;

But since FormatStr uses the Drivers unit which isn't part of TPW this
no longer works.

Thanks in advance.
--
Dave

 

Re:Converting a decimal to a hex string


Quote
Dave wrote:

> How can I convert an byte to a hex string in TPW v1.5?
> In TP v7 (dos) I used to use:

> Function Dec2Hex (decno : integer) : string;
> Var
>    workinghex : string;
> Begin
> FormatStr(Workinghex, '%2x',decno);
> Dec2Hex := workinghex;
> End;

> But since FormatStr uses the Drivers unit which isn't part of TPW this
> no longer works.

> Thanks in advance.
> --
> Dave

Hi Dave,

in an older unit we used to use in the past I have found the following
solution :

  function HexB( b : byte ) : string;
  const
    HexChar : array[0..15] of char = '0123456789ABCDEF';
  begin
    HexB := HexChar[b shr 4] + HexChar[b and $0F];
  end;

This function was used with Turbo Pascal 7.0, but I think, its working
in TPW, too.

Bye,

Carmen

Re:Converting a decimal to a hex string


Here you have it:

type st4 : string[4];

function Word2Hex(w : word) : st4;
var hexStr : st4;

  function Translate(b : byte) : char;
  begin
    if b<10 then
      translate:=chr(b+48)
    else
      translate:=chr(b+45);
  end;

begin   { of  Word2Hex }
  hexstr:='';
  hexstr:=hexstr+translate(hi(w) shr 4);
  hexstr:=hexstr+translate(hi(w) and 15);
  hexstr:=hexstr+translate(lo(w) shr 4);
  hexstr:=hexstr+translate(lo(w) and 15);
  word2hex:=hexstr;
end;

As you see, it's not that hard. Just think in a base-16-manner. You could also do a
function with div/mod. Look it up in your book.

// Tobias

Re:Converting a decimal to a hex string


In article <31FDED13....@algonet.se> of Tue, 30 Jul 1996 13:08:03 in
comp.lang.pascal.borland, Tobias Andersson <drg...@algonet.se> wrote:

Quote

>    if b<10 then
>      translate:=chr(b+48)
>    else
>      translate:=chr(b+45);

If you think of it (and then write it) as

        if b<10 then translate := char(b   +Ord('0'))
                else translate := char(b-10+Ord('A')) ;

you will avoid putting 45 where you mean 55 !

The 55 will of course be calculated at compile time.

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

Re:Converting a decimal to a hex string


In article <4tinn9$...@news00.btx.dtag.de>,

Quote
CarmenSmolne.Herrenkne...@t-online.de (Carmen Smolne) wrote:
> Dave wrote:

> > How can I convert an byte to a hex string in TPW v1.5?
> > In TP v7 (dos) I used to use:

> > Function Dec2Hex (decno : integer) : string;
> > Var
> >    workinghex : string;
> > Begin
> > FormatStr(Workinghex, '%2x',decno);
> > Dec2Hex := workinghex;
> > End;

> > But since FormatStr uses the Drivers unit which isn't part of TPW this
> > no longer works.

> > Thanks in advance.
> > --
> > Dave

I'm at home and don't have any manuals handy, but "wvsprinf" may be
what you're looking for in TPW.  Something like:

  function TPW_HexS (decno : integer) : string;
  var
    szWbuf : array[0..10] of char;  
  begin
     wvsprinf(szWbuf,'%2x',decno);
     TPW_HexS := StrPas(szBuf);    { StrPas to yield Pascal string }
  end;

Jim

--

You have single-handedly brought the world
to the brink of nuclear destruction!
Bad kitty! Bad kitty!

Other Threads