Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Can you help me. if convert binary to octal anyone can help me..???

this code convert octal to binary. help me for change this code.

function oct2bin returns character ( input octalString as character ):

  define variable i      as integer   no-undo.
  define variable n      as integer   no-undo.
  define variable c      as character no-undo.
  define variable result as character no-undo.

  n = length( octalString ).

  if n < 2 or substring( octalString, 1, 1 ) <> "~" then        /* a valid octalString begins with ""                                */
    do:
      message "valid octal strings must begin with ~".
      result = ?.
    end.
   else
    do i = 2 to n:

      c = substring( octalString, i, 1 ).

      if asc( c ) < 48 or asc( c ) > 55 then                        /* a valid octalString only contains the digits 0 thru 7        */
        do:
          message c "is not a valid numeric character".
          result = ?.
          leave.
        end.
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
250 views
Welcome To Ask or Share your Answers For Others

1 Answer

Groups of three bits can be converted into an octal digit. Divide the binary number into three-bit groups, convert the group to an octal digit, then string the digits into an octal number.

PROCEDURE BinToOct:

    DEFINE INPUT  PARAMETER pcBinStr AS CHARACTER NO-UNDO.
    DEFINE OUTPUT PARAMETER pcOctStr AS CHARACTER NO-UNDO.
    
    DEFINE VARIABLE cPadding  AS CHARACTER NO-UNDO.
    DEFINE VARIABLE iLoop     AS INTEGER   NO-UNDO.
    DEFINE VARIABLE cBinDigit AS CHARACTER NO-UNDO.
    DEFINE VARIABLE cOctDigit AS CHARACTER NO-UNDO.
    
    /* Pad input with zeroes to make it divisible by 3. */
    IF LENGTH(pcBinStr) MOD 3 <> 0 THEN
    ASSIGN
        cPadding = FILL("0", (3 - LENGTH(pcBinStr) MOD 3))
        pcBinStr = cPadding + pcBinStr.
    
    /* Divide up bits into groups of 3. */
    DO iLoop = 1 TO LENGTH(pcBinStr) BY 3:
        cBinDigit = SUBSTRING(pcBinStr, iLoop, 3).
        
        /* Convert bits to octal digit. */
        CASE cBinDigit:
            WHEN "000" THEN cOctDigit = "0".
            WHEN "001" THEN cOctDigit = "1".
            WHEN "010" THEN cOctDigit = "2".
            WHEN "011" THEN cOctDigit = "3".
            WHEN "100" THEN cOctDigit = "4".
            WHEN "101" THEN cOctDigit = "5".
            WHEN "110" THEN cOctDigit = "6".
            WHEN "111" THEN cOctDigit = "7".
            OTHERWISE MESSAGE "Bad input" VIEW-AS ALERT-BOX ERROR.
        END CASE.
        
        /* Add digit to result string. */
        pcOctStr = pcOctStr + cOctDigit.
    END.
    
END PROCEDURE.


DEFINE VARIABLE cResult AS CHARACTER NO-UNDO.

RUN BinToOct (INPUT "1010011100101", OUTPUT cResult).

MESSAGE cResult VIEW-AS ALERT-BOX INFORMATION.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...