# Board index » delphi » BINARY TO DECIMAL PGM

## BINARY TO DECIMAL PGM

I'm trying to write a procedure to change a binary number { 0101 } to a
decimal { 5 } ( This is just an example ) . I'm entering the binary
number like this 0101 .
Any help or procedure would be greatly appreciated.
Mike in NJ
-
MICHAEL P.  GXHH...@prodigy.com

## Re:BINARY TO DECIMAL PGM

Using TP7:
I'm trying to write a procedure to change a binary number { 0101 } to a
decimal { 5 } ( This is just an example ) . I'm entering the binary
number like this 0101 .
Any help or procedure would be greatly appreciated.
Mike in NJ
-
MICHAEL P.  GXHH...@prodigy.com

## Re:BINARY TO DECIMAL PGM

##### Quote
> Using TP7:
> I'm trying to write a procedure to change a binary number { 0101 } to a
> decimal { 5 } ( This is just an example ) . I'm entering the binary
> number like this 0101 .

Just an iterative process of multiplying increasing powers of 2 by
the digit character:
The above value is computed as (1x2 to the 0th)+(0x2 to the 1st)+1x2
to the 2nd)+(0x2 to the 3rd).  The result is 5. That's processing the
digit characters right-to-left, of course...

## Re:BINARY TO DECIMAL PGM

In article <5d5u2c\$1...@usenetz1.news.prodigy.com>,

##### Quote
Michael Passero <GXHH...@prodigy.com> wrote:

:I'm trying to write a procedure to change a binary number { 0101 } to a
:decimal { 5 } ( This is just an example ) . I'm entering the binary
:number like this 0101 .

Your problem is covered, with source code, in

112348 Nov 12 1996 ftp://garbo.uwasa.fi/pc/link/tsfaqp.zip
tsfaqp.zip Common Turbo Pascal Questions and Timo's answers

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  <URL:http://uwasa.fi/~ts>  ; FIN-65101,  Finland

## Re:BINARY TO DECIMAL PGM

##### Quote
Michael Passero wrote:

> I'm trying to write a procedure to change a binary number { 0101 } to a
> decimal { 5 } ( This is just an example ) . I'm entering the binary
> number like this 0101 .
>            Any help or procedure would be greatly appreciated.
>                                              Mike in NJ

Try something like:

function bin2dec (s : string) : longint;

var
tmp : longint;
p : byte;

begin
tmp := 0;
for p := length(s) downto 1 do
begin
inc (tmp,(ord(s[length(s)])-48) shl p);
dec (s[0]);
end;
bin2dec := tmp;
end;

Untested, but I think it should work for strings up to 31 characters.
The 32nd bit in a longint is used for sign (positive/negative), and the
binary representation is a bit different for negative numbers.

##### Quote
> -
>   MICHAEL P.  GXHH...@prodigy.com

--
Scott Earnest        | We now return you to our regularly |
set...@ix.netcom.com | scheduled chaos and mayhem. . . .  |

## Re:BINARY TO DECIMAL PGM

In article <32F6FC06....@ix.netcom.com> of Tue, 4 Feb 1997 01:06:14 in
comp.lang.pascal.borland, Scott Earnest <set...@ix.netcom.com> wrote:

##### Quote
>Michael Passero wrote:

>> I'm trying to write a procedure to change a binary number { 0101 } to a
>> decimal { 5 } ( This is just an example ) . I'm entering the binary
>> number like this 0101 .
>>            Any help or procedure would be greatly appreciated.
>>                                              Mike in NJ

>Try something like:

>function bin2dec (s : string) : longint;

>var
>  tmp : longint;
>  p : byte;

>begin
>  tmp := 0;
>  for p := length(s) downto 1 do
>    begin
>      inc (tmp,(ord(s[length(s)])-48) shl p);
>      dec (s[0]);
>    end;
>  bin2dec := tmp;
>end;

>Untested, but I think it should work for strings up to 31 characters.
>The 32nd bit in a longint is used for sign (positive/negative), and the
>binary representation is a bit different for negative numbers.

There is of course no "decimal" in "bin2dec" - the complete answer to
the question as asked would in addition need use of Str or equivalent.

Your code gives the wrong result in many cases; for a start, the shift
amount is wrong.

Your method involves shifting by all amounts from 1 to length(s).  It's
amazing how often the simple " * base and add " loop is missed.

This seems simpler (lightly tested) and appears to work for all 32 bits.

function bin2longint(const s : string) : longint ;
var tmp : longint ; p : byte ;
begin tmp := 0 ;
for p := 1 to length(s) do
tmp := (tmp shl 1) {in general, tmp := tmp*BASE} + (Ord(s[p])-48) ;
bin2longint := tmp end ;

SHL gets round the range checking, which was ON; I suspect *BASE would
not.
--
John Stockton, Surrey, UK.  j...@merlyn.demon.co.uk  Turnpike v1.12  MIME
Web URL: http://www.merlyn.demon.co.uk/
Standard signature separator is as above, a line containing "-- " (SoRFC1036)

## Re:BINARY TO DECIMAL PGM

##### Quote
GXHH...@prodigy.com (Michael Passero) wrote:
>I'm trying to write a procedure to change a binary number { 0101 } to a
>decimal { 5 } ( This is just an example ) . I'm entering the binary
>number like this 0101 .
>           Any help or procedure would be greatly appreciated.
>                                             Mike in NJ
>-
>  MICHAEL P.  GXHH...@prodigy.com

function BinaryStringToDecimal(s: String): longint;
var c : char;
i : byte;
l : byte absolute s; {= length of s}
n : longint;
function NxtChr: char;
begin
if i=l then NxtChr:=#0 else begin
i:=i+1;
NxtChr:=s[i];
end
end;
begin
i:= 0; n:= 0; c:= NxtChr;
while (c='0') or (c='1') do begin
n:= 2*n + ord(c)-ord('0');
c:= NxtChr;
end;
BinaryStringToDecimal:= n;
end;

For speed, you can change 2*n into n+n, or (n shl 1).
I think there is no need to change ord('0') into 48 since this
optimizaion is probably done by the compiler.

-----------------------------------------
J.R. Ferguson, Amsterdam, The Netherlands
e-mail: j.r.fergu...@iname.com
web: www.xs4all.nl/~ferguson
Freeware for DOS and Windows

## Re:BINARY TO DECIMAL PGM

##### Quote
Michael Passero wrote:

> Using TP7:
> I'm trying to write a procedure to change a binary number { 0101 } to a
> decimal { 5 } ( This is just an example ) . I'm entering the binary
> number like this 0101 .
>            Any help or procedure would be greatly appreciated.
>                                              Mike in NJ
> -
>   MICHAEL P.  GXHH...@prodigy.com

I wrote something like that for THE TOOLBOX.
Go to my homepage (check the footer/autosig)
and to the "The Toolbox" area.

you're looking for HEX

--
--------------------------------------------------------------
/------\/-----\/-----\/-\  /-\/------\/-----\/-----\/---\  /-\
\| /-\ || /-\ || /-\ || | /  /| /\/\ || /-\ || /-\ ||    \ | |
| | | || \_/ || \_/ || |/ /_ | |||| || | | || | | || |\  \| |
| | | || /-\ || /-\| | /-\  || |\/| || | | || | | || | \  \ |
/| \-/ || | | || | | \| | |  || |  | || \_/ || \_/ || |  \   |
\______/\_/ \_/\_/ \_/\_/ \__/\_/  \_/\_____/\_____/\_/   \__/
--------- http://connexus.apana.org.au/~mikuto/darky ---------

## Re:BINARY TO DECIMAL PGM

##### Quote
Dr John Stockton wrote:
> [...]
> >Untested, but I think it should work for strings up to 31 characters.
> >The 32nd bit in a longint is used for sign (positive/negative), and the
> >binary representation is a bit different for negative numbers.

> There is of course no "decimal" in "bin2dec" - the complete answer to
> the question as asked would in addition need use of Str or equivalent.

I'm not sure I understand what you mean by this.

##### Quote
> Your code gives the wrong result in many cases; for a start, the shift
> amount is wrong.

Yes, the potential errors of writing code off the cuff can be good.  I
went back and retested, and found that it's wrong (at least I mentioned
that it was untested :-).  Here's what I came up with to resolve the
error(s):

function bin2dec (s : string) : longint;

var
tmp : longint;
p : byte;

begin
tmp := 0;
for p := 1 to length(s) do
begin
inc (tmp,longint(ord(s[length(s)])-48) shl pred(p));
dec (s[0]);
end;
bin2dec := tmp;
end;

This one *is* tested (under TP6 and TP7).

##### Quote
> Your method involves shifting by all amounts from 1 to length(s).  It's
> amazing how often the simple " * base and add " loop is missed.

I *know* that's a way to do it, and a way I've done it in the past.  I
specifically did it that way for a reason -- in case that was a homework
problem, I figured I'd produce a sufficiently obfuscated bit of code
that might make a person think twice about asking outright for a snippet
of code to stand in for what s/he should be doing for her/himself.

##### Quote
> This seems simpler (lightly tested) and appears to work for all 32 bits.

> function bin2longint(const s : string) : longint ;
> var tmp : longint ; p : byte ;
> begin tmp := 0 ;
>   for p := 1 to length(s) do
>     tmp := (tmp shl 1) {in general, tmp := tmp*BASE} + (Ord(s[p])-48) ;
>   bin2longint := tmp end ;

Okay, it does appear to work.  Also tested mine accordingly and it does
work for all 32 bits.

##### Quote
> SHL gets round the range checking, which was ON; I suspect *BASE would
> not.

SHL shouldn't get around range/overflow checking, AFAIK.  I believe it's
also subject to scrutiny.  In either case, both correctly converted a
string of 32 1's to -1, regardless of whether range and overflow
checking were enabled.

##### Quote
> --
> John Stockton, Surrey, UK.  j...@merlyn.demon.co.uk  Turnpike v1.12  MIME
>   Web URL: http://www.merlyn.demon.co.uk/
>   Standard signature separator is as above, a line containing "-- " (SoRFC1036)

--
Scott Earnest        | We now return you to our regularly |
set...@ix.netcom.com | scheduled chaos and mayhem. . . .  |

## Re:BINARY TO DECIMAL PGM

In article <32F9C391.2...@ix.netcom.com> of Thu, 6 Feb 1997 03:42:09 in
comp.lang.pascal.borland, Scott Earnest <set...@ix.netcom.com> wrote:

##### Quote
>Dr John Stockton wrote:

>> There is of course no "decimal" in "bin2dec" - the complete answer to
>> the question as asked would in addition need use of Str or equivalent.

>I'm not sure I understand what you mean by this.

Bin2dec converts from a binary string representation to a longint.  There's
nothing decimal about a longint, except for the way that Write represents
it to base 10.  If we had had five fingers on each hand, rather than four,
we'd be counting to base 12, Write would convert to base 12, but a longint
would still be four bytes and a binary string would be unchanged.

My bin2dec was :-

##### Quote
>> function bin2longint(const s : string) : longint ;
>> var tmp : longint ; p : byte ;
>> begin tmp := 0 ;
>>   for p := 1 to length(s) do
>>     tmp := (tmp shl 1) {in general, tmp := tmp*BASE} + (Ord(s[p])-48) ;
>>   bin2longint := tmp end ;

All we actually need to do is collect the LSBs of the string, presuming
that the string contains only '0' & '1', in the longint.

A simple change to :-

function bin2longint(const s : string) : longint ;
var tmp : longint ; p : byte ;
begin tmp := 0 ;
for p := 1 to length(s) do
tmp := (tmp shl 1) or (byte(s[p]) and 1) ;
bin2longint := tmp end ;

(untested) will do the job merely by masking, copying, and moving bits; the
only arithmetic is administrative, and that can be more or less eliminated
(untested) :-

function bin2longint(const s : string) : longint ;
var tmp : longint ; p : byte ;
begin tmp := 0 ;
while s<>'' do begin
tmp := (tmp shl 1) or (byte(s[1]) and 1) ; Delete(s, 1, 1) end ;
bin2longint := tmp end ;

--
John Stockton, Surrey, UK.  j...@merlyn.demon.co.uk  Turnpike v1.12  MIME
Web URL: http://www.merlyn.demon.co.uk/
Standard signature separator is as above, a line containing "-- " (SoRFC1036)