<< Previous Message Main Index Next Message >>
<< Previous Message in Thread This Month Next Message in Thread >>
Date   : Tue, 25 Dec 2007 13:30:34 +0000
From   : jgh@... (Jonathan Graham Harston)
Subject: Octal and Binary output

A Christmas programming challenge.
 
I've had the following code to output in binary and octal lying
around in programs for years and have never been able to get it to
work 100% correctly. Specifically, very large negative numbers,
with b31=1 and b30=0, convert incorrectly.
 
REM Binary padded with zeros
DEFFNb0(A%,N%):LOCAL A$,B$
IFA%<0:B$="1":A%=A% AND &7FFFFFFF ELSE B$="0"
REPEAT A$=STR$(A% AND 1)+A$:A%=A% DIV 2:UNTILA%=0
=RIGHT$(STRING$(N%,B$)+A$,N%)
:
REM Octal padded with zeros
DEFFNo0(A%,N%):LOCAL A$,B$
IFA%<0:B$="7":A%=A% AND &7FFFFFFF ELSE B$="0"
REPEAT A$=STR$(A% AND 7)+A$:A%=A% DIV 8:UNTIL A%=0
=RIGHT$(STRING$(N%,B$)+A$,N%)
 
Thas has never been a problem in my programs as I've never used
them to convert anything larger than 16-bit. For example,
FNo0(&FFFF,6) correctly returns 177777 and FNb0(&FFFF,16)
correctly returns 1111111111111111.
 
The challenge, if you choose to accept it, is to fix the above two
routines to correctly convert large 32-bit negatives. Points will
be awarded for neatness and elegance, and an extra Christmas Star
for a hand-meets-forehead of-course solution.
 
-- 
J.G.Harston - jgh@...                - mdfs.net/User/JGH
BBC BASIC for Windows and Internationalisation
  See http://mdfs.net/Software/BBCBasic/Windows/ProgTips
<< Previous Message Main Index Next Message >>
<< Previous Message in Thread This Month Next Message in Thread >>