Subject: | |
From: | |
Reply To: | |
Date: | Fri, 17 Nov 2000 14:29:12 -0800 |
Content-Type: | text/plain |
Parts/Attachments: |
|
|
At 2:51 PM -0500 11/17/00, Stigers, Greg [And] wrote:
>Try this. I'm sure there's room for improvement. It does not clean up after
>itself, and it does not correctly handle numbers greater than 255, if that
>is needed.
>
>:PRINT DECTOBIN.XEQ
>PARM PARM=" "
>SETVAR NUMBER !PARM
>SETVAR STRING ""
>WHILE NUMBER <> 0
> SETVAR STRING "![!NUMBER MOD 2]"+"!STRING"
> SETVAR NUMBER NUMBER/2
>ENDWHILE
>SETVAR STRING RHT("00000000"+"!STRING",8)
>ECHO !STRING
I've shamelessly stolen a couple of Greg's ideas and improved upon them
slightly. This new submission (and I promise it'll be my last) adds a
second optional parameter to specify the output length. If the length of
the binary output string is less than the specified length, then the output
is padded with as many leading zeroes as are required in order to make it
the specified length.
:print num2bin
parm innum=0, fill=0
setvar tnum !innum
setvar binval "![tnum mod 2]"
while setvar(tnum,tnum/2) > 0
setvar binval "![tnum mod 2]" + binval
endwhile
if !fill > 0 and len(binval) < !fill
setvar binval rht(rpt('0',!fill) + binval, !fill)
endif
echo !binval
:num2bin
0
:num2bin ,8
00000000
:num2bin 5
101
:num2bin 5,2
101
:num2bin 5,8
00000101
:num2bin 4095
111111111111
:num2bin 4095,8
111111111111
:num2bin 4005,16
0000111111111111
:num2bin 2147483647
1111111111111111111111111111111
:num2bin 2147483647,32
01111111111111111111111111111111
:
Isn't CI scripting fun!
Barry Lake [log in to unmask]
Allegro Consultants, Inc. www.bogus.allegro.com
(408)252-2330
--------
Attempting to reduce spam influx.
Remove ".bogus" to create valid address.
|
|
|