Pages

Tuesday, October 23, 2012

Decimal to Binary function in Powerbuilder

After Binary to Decimal function posted several weeks ago, I try to make Decimal to Binary function with Powerbuilder


Create function name: f_dectobin, set String as return value, and add iDec as parameter. iDec should be Integer type, and choose Value as Pass By.

Then add script below

1:  boolean bDone  
2:  string sBin  
3:  int i  
4:    
5:  IF iDec = 0 OR iDec = 1 THEN  
6:       sBin = String(iDec)  
7:  ELSE  
8:       DO WHILE NOT bDone  
9:            i = Mod(iDec, 2)  
10:            iDec = Int(iDec / 2)  
11:            sBin = String(i) + sBin  
12:            IF iDec = 1 THEN  
13:                 bDone = TRUE  
14:                 sBin = "1" + sBin  
15:            END IF  
16:       LOOP  
17:  END IF  
18:    
19:  sBin = Right("0000000" + sBin, 8)  
20:    
21:  RETURN sBin  

No comments:

Post a Comment