Board index » delphi » Hex -> Decimal

Hex -> Decimal

I'm searching for an algorithm to transform a hex string back to a
decimal number; i.e. an 8 byte string to a longint.
Suppose I have the string s = 'ABCD1234'. Interpreting it as a
hexadecimal value, how can I transform it into a longint?

Oh, by the way... This is not homework. It's merely to fiddle a bit
around with serial numbers on diskettes.

 

Re:Hex -> Decimal


Quote
Frederic wrote:

> I'm searching for an algorithm to transform a hex string back to a
> decimal number; i.e. an 8 byte string to a longint.
> Suppose I have the string s = 'ABCD1234'. Interpreting it as a
> hexadecimal value, how can I transform it into a longint?

> Oh, by the way... This is not homework. It's merely to fiddle a bit
> around with serial numbers on diskettes.

http://www.geocities.com/SiliconValley/2926/tp.html
chapter "numerics", "RadixVal" or "ValRadix". Forgot the name :-)

Regards, Franz Glaser

Re:Hex -> Decimal


On Mon, 30 Nov 1998 16:32:02 +0100, Frederic <frede...@rz-online.de>
wrote:

Quote
> I'm searching for an algorithm to transform a hex string back to a
> decimal number; i.e. an 8 byte string to a longint.
> Suppose I have the string s = 'ABCD1234'. Interpreting it as a
> hexadecimal value, how can I transform it into a longint?

Then you want to transform a hex string to a longint. Longints don't
contain "decimal numbers". They contain binary numbers ;-)

The procedure VAL does what you want. It is used by the system when
you type in a hex string.

const
 s:string='1234ABCD';
var
  l:longint;
  errpos:integer;
begin
  val('$'+s,l,errpos);
  if errpos>0 then writeln('SHIT');
  writeln(l)
end.

Regards
Horst

Re:Hex -> Decimal


In article <3662BA72.95D1...@rz-online.de>,

Quote
Frederic  <frede...@rz-online.de> wrote:
>I'm searching for an algorithm to transform a hex string back to a
>decimal number; i.e. an 8 byte string to a longint.
>Suppose I have the string s = 'ABCD1234'. Interpreting it as a
>hexadecimal value, how can I transform it into a longint?

var err:integer;
    x:longint;
...

Val('$'+s,x,err);

Osmo

Re:Hex -> Decimal


Quote
> I'm searching for an algorithm to transform a hex string back to a
> decimal number; i.e. an 8 byte string to a longint.
> Suppose I have the string s = 'ABCD1234'. Interpreting it as a
> hexadecimal value, how can I transform it into a longint?

I found an undocumented (?) solution.
Just put a '$' before the hex number and convert it with val().
:)

Andras

Re:Hex -> Decimal


Update draft:

-Subject: Converting the number base

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.

 A2: In the case of converting a hexadecimal string into a decimal
number another option can be used, i.e. the Val function:
  (* Convert hexadecimal string to a decimal value *)
  function HEX2DEC (hex: string) : longint;
  var dec : longint;
      k   : integer;
  begin
    Val ('$'+hex, dec, k);
    if k = 0 then
      if dec < 0 then begin
        writeln (hex, ' out of range (longint becomes negative)');
        halt(1);
        end
      else hex2dec := dec
    else begin
      writeln (hex, ' error or out of range');
      halt(1);
      end;
  end;
--------------------------------------------------------------------

   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.
Advice on spam foiling at http://www.uwasa.fi/~ts/info/spamfoil.html

Re:Hex -> Decimal


JRS:  In article <7412er$...@loisto.uwasa.fi> of Tue, 1 Dec 1998
17:36:59 in news:comp.lang.pascal.borland, Timo Salmi <t...@UWasa.Fi>
wrote:

Quote
>Update draft:

>-Subject: Converting the number base

>42. *****
> Q: How do I convert a decimal word into a hexadecimal string, etc?
>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 *)

Granted that nowadays "shr i" is as fast as "shr 1" (* PLEASE change "i"
to "j", for those with ageing eyes! *), it's still inelegant to do all
those multi-shifts.

How about (untested, but very like part of my pas-opts.htm; over-short
names)

function LB(D : longint) : string ;
const BD : array [boolean] of char = '01' ;
var j : byte ; S : string ;
begin {omit FillChar}
  S[0] := #32 ;
  for j := 32 downto 1 do begin
    S[j] := BD[Odd(D)] ; D := D shr 1 end ;
  LB := S end ;

?

--
John Stockton, Surrey, UK.    j...@merlyn.demon.co.uk    Turnpike v4.00    MIME.
  Web <URL: http://www.merlyn.demon.co.uk/> - TP/BP/&c. FAQqish topics & links.
  Timo's TurboPascal <A HREF="ftp://garbo.uwasa.fi/pc/link/tsfaqp.zip">FAQ</A>.
  <A HREF="http://www.merlyn.demon.co.uk/clpb-faq.txt">Mini-FAQ</A> of c.l.p.b.

Re:Hex -> Decimal


In article <1KpXZfBsfHZ2E...@merlyn.demon.co.uk>, Dr John Stockton
<j...@merlyn.demon.co.uk> writes

Quote
>JRS:  In article <7412er$...@loisto.uwasa.fi> of Tue, 1 Dec 1998
>17:36:59 in news:comp.lang.pascal.borland, Timo Salmi <t...@UWasa.Fi>
>wrote:
>>Update draft:

>>-Subject: Converting the number base

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

>>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), ' ');

On this occasion FillChar is not needed

- Show quoted text -

Quote
>>    binar[0] := chr(32);
>>    for i := 0 to 31 do
>>      binar[32-i] := BinDigit[(decimal shr i) and 1];
>>    lbinfn := binar;
>>  end;  (* lbinfn *)

>Granted that nowadays "shr i" is as fast as "shr 1" (* PLEASE change "i"
>to "j", for those with ageing eyes! *), it's still inelegant to do all
>those multi-shifts.

>How about (untested, but very like part of my pas-opts.htm; over-short
>names)

>function LB(D : longint) : string ;
>const BD : array [boolean] of char = '01' ;
>var j : byte ; S : string ;
>begin {omit FillChar}
>  S[0] := #32 ;
>  for j := 32 downto 1 do begin
>    S[j] := BD[Odd(D)] ; D := D shr 1 end ;
>  LB := S end ;

Certainly a far neater algorithm and works fine to give the right answer
- saves about 3 bytes of code if FillChar is removed from LBINFN but no
speed saving on execution.

Personally I'd go for:

  function LB(d : longint) : String;
  const BD : array[boolean] of char = '01';
  var j : byte; S: string;
  begin
       S[0] := #32;
       for j := 0 to 31 do
       S[32-j] := BD[d and (1 shl j) <> 0];
       LB := s;
  end;

Although no byte saving on Timo's version without FillChar, it does
execute around 35-40% faster than the two versions above.

--
Pedt

Kill one Scottish midge and a million will come to the funeral

Re:Hex -> Decimal


In article <OV6yiIA2KJZ2E...@pedt.demon.co.uk>,
Pedt Scragg  <newsmas...@pedt.demon.co.uk> wrote:
:>>    FillChar (binar, SizeOf(binar), ' ');
:On this occasion FillChar is not needed

True, but I prefer to put it there routinely, especially in the FAQ.

:  function LB(d : longint) : String;
:  const BD : array[boolean] of char = '01';
:  var j : byte; S: string;
:  begin
:       S[0] := #32;
:       for j := 0 to 31 do
:       S[32-j] := BD[d and (1 shl j) <> 0];
:       LB := s;
:  end;
:
:Although no byte saving on Timo's version without FillChar, it does
:execute around 35-40% faster than the two versions above.

I'll include this as a joint alternative from John and you.

   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.
Advice on spam foiling at http://www.uwasa.fi/~ts/info/spamfoil.html

Other Threads