Board index » delphi » BINARY TO DECIMAL PGM
GXHH...@prodigy.com (Michael Passero)
![]() Delphi Developer |
Wed, 18 Jun 1902 08:00:00 GMT
|
GXHH...@prodigy.com (Michael Passero)
![]() Delphi Developer |
Wed, 18 Jun 1902 08:00:00 GMT
BINARY TO DECIMAL PGMI'm trying to write a procedure to change a binary number { 0101 } to a |
Michael Passe
![]() Delphi Developer |
Wed, 18 Jun 1902 08:00:00 GMT
Re:BINARY TO DECIMAL PGMUsing TP7: |
Mike Copelan
![]() Delphi Developer |
Wed, 18 Jun 1902 08:00:00 GMT
Re:BINARY TO DECIMAL PGMQuote> Using TP7: 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... |
Timo Sal
![]() Delphi Developer |
Wed, 18 Jun 1902 08:00:00 GMT
Re:BINARY TO DECIMAL PGMIn article <5d5u2c$1...@usenetz1.news.prodigy.com>, QuoteMichael Passero <GXHH...@prodigy.com> wrote: :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 All the best, Timo .................................................................... |
Scott Earnes
![]() Delphi Developer |
Wed, 18 Jun 1902 08:00:00 GMT
Re:BINARY TO DECIMAL PGMQuoteMichael Passero wrote: function bin2dec (s : string) : longint; var begin Untested, but I think it should work for strings up to 31 characters. Quote> - Scott Earnest | We now return you to our regularly | set...@ix.netcom.com | scheduled chaos and mayhem. . . . | |
Dr John Stockto
![]() Delphi Developer |
Wed, 18 Jun 1902 08:00:00 GMT
Re:BINARY TO DECIMAL PGMIn article <32F6FC06....@ix.netcom.com> of Tue, 4 Feb 1997 01:06:14 in Quote>Michael Passero wrote: 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 Your method involves shifting by all amounts from 1 to length(s). It's This seems simpler (lightly tested) and appears to work for all 32 bits. function bin2longint(const s : string) : longint ; SHL gets round the range checking, which was ON; I suspect *BASE would |
J.R. Fergus
![]() Delphi Developer |
Wed, 18 Jun 1902 08:00:00 GMT
Re:BINARY TO DECIMAL PGMQuoteGXHH...@prodigy.com (Michael Passero) wrote: 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). ----------------------------------------- |
DarkMoon Risin
![]() Delphi Developer |
Wed, 18 Jun 1902 08:00:00 GMT
Re:BINARY TO DECIMAL PGMQuoteMichael Passero wrote: Go to my homepage (check the footer/autosig) and to the "The Toolbox" area. It's a pretty small download. -- |
Scott Earnes
![]() Delphi Developer |
Wed, 18 Jun 1902 08:00:00 GMT
Re:BINARY TO DECIMAL PGMQuoteDr John Stockton wrote: Quote> Your code gives the wrong result in many cases; for a start, the shift 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 begin This one *is* tested (under TP6 and TP7). Quote> Your method involves shifting by all amounts from 1 to length(s). It's 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. work for all 32 bits. Quote> SHL gets round the range checking, which was ON; I suspect *BASE would 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> -- Scott Earnest | We now return you to our regularly | set...@ix.netcom.com | scheduled chaos and mayhem. . . . | |
Dr John Stockto
![]() Delphi Developer |
Wed, 18 Jun 1902 08:00:00 GMT
Re:BINARY TO DECIMAL PGMIn article <32F9C391.2...@ix.netcom.com> of Thu, 6 Feb 1997 03:42:09 in Quote>Dr John Stockton wrote: 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 ; that the string contains only '0' & '1', in the longint. A simple change to :- function bin2longint(const s : string) : longint ; (untested) will do the job merely by masking, copying, and moving bits; the function bin2longint(const s : string) : longint ; -- |