Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Converting a Decmial to Binary in Crystal 8.5

Status
Not open for further replies.

ksammie

Technical User
Feb 1, 2002
2
US
I am writing a report that needs to pull a number from a database and then convert it to binary. This is being used to determine different options in our application. I need to display which options are installed on a System Options Report.

I appreciate any help I can get!!
 
The following will convert a number into binary with leading 0's Change the 12 below (in bold) to the length of the string you want.


stringvar bin;numbervar power;numbervar loop;numbervar loopnum;
numbervar remain:={number.field};

for loop:=12 to 0 step -1 do(

if loopnum>=(2^loop) then (remain:=loopnum-(2^loop);
bin:= bin+"1")
else (remain:=remain; bin:=bin+"0") ;
loopnum:=remain);
bin Mike

 
*******REVISION*******
"loopnum:=remain" should not be at the end of the loop, it should be at the beginning. With it being at the end, the formula works fine for smaller numbers (numbers> 2^loop), but not the larger ones. The bin variable should have been a "local"

Corrected formula:

local stringvar bin;numbervar power;numbervar loop;numbervar loopnum;
numbervar remain:={number.field};

for loop:=12 to 0 step -1 do(loopnum:=remain

if loopnum>=(2^loop) then (remain:=loopnum-(2^loop);
bin:= bin+"1")
else (remain:=remain; bin:=bin+"0") ;
loopnum:=remain);
bin Mike

 
if you are trying to pad a number with leading zeros the following should work:

ReplicateString("0",12-length(Totext({numberfield},0,""}))

again, change the 12 to any value you desire. Software Support for Sage Mas90, Macola, Crystal Reports, Goldmine and MS Office
 
Thanks for the formula it works great for one of my reports. I am now trying to do the same conversion for another report only this time I am converting negative decimal numbers to binary. Any suggestions??

Thanks Again for the great formula.

Michelle
 
How do you express the binary as a negative? Excel (which limits it dec2bin function to -512 to 511) does it buy having 10 positions with the first being a 1.

I've never worked with negative binaries so I'm not sure why, but -1 is 1111111111. (???) Mike

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top