1

octal numbers to binary numbers.

I want to convert this code to octal to binary how ..???

help me.

define variable bin as integer.
define variable dec as integer.

set dec label "Masukkan angka desimal" with side-labels.

repeat:
    bin = dec modulo 2.
    dec = dec / 2.
    if bin = 1 then
        dec = dec - 1.
    else if dec < 1 then
        quit.
    display bin dec with frame a down.
end.
A Myf
  • 11
  • 3
  • 1
    Your code seems to convert from decimal to binary but you ask about octal? – Jensd Jun 23 '21 at 10:54
  • Progress-4gl does not have pre-built "octal" or a "binary" datatypes. The integer datatype is for base-10 integers. It is common for octal numbers to be written with a "\" prefix. Thus \033 is the octal representation of character 27 or ESCAPE. Another common base is hexadecimal which is usually written with a 0x prefix. So ESCAPE in hex would be 0x1b. Binary numbers are very recognizable so they usually don't get written with a prefix but they are sometimes grouped into 4 or 8 digits corresponding to a "nibble" or a "byte". For instance: 0001 1011 would be ESCAPE. – Tom Bascom Jun 29 '21 at 18:40

1 Answers1

1
/* oct2bin.p
 *
 */

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.

      case c:
        when "0" then result = result + "000".
        when "1" then result = result + "001".
        when "2" then result = result + "010".
        when "3" then result = result + "011".
        when "4" then result = result + "100".
        when "5" then result = result + "101".
        when "6" then result = result + "110".
        when "7" then result = result + "111".
      end.

      /* message octalString n c result.    */
      /* pause.                             */

    end.

  return result.

end.

    
define variable inputField as character no-undo.

do while true:
  update inputField.
  display oct2bin( inputField ).
end.
Tom Bascom
  • 13,405
  • 2
  • 27
  • 33
  • I want the result, what number I input then that's the result that comes out. – A Myf Jun 28 '21 at 01:49
  • can you help me again :) – A Myf Jun 28 '21 at 03:19
  • I am sorry but I don’t understand your question. Could you be more detailed about what you are trying to do? – Tom Bascom Jun 28 '21 at 11:23
  • This question along with your previous Pascal's Triangle question, has the flavor of homework. I don't mind helping with that but it might help if you post the actual assignment. Or, if it is not homework, the requirements statement from whomever is requesting this feature would help. – Tom Bascom Jun 28 '21 at 20:58
  • So, I want to output the result using the input I entered, like if I enter 4 octal number then the binary result is 100. This is what I want sir – A Myf Jun 29 '21 at 02:41
  • I have updated the answer to prompt for a value and output the result. – Tom Bascom Jun 29 '21 at 18:26
  • can you help me again sir. if convert binary to octal how sir can help me again..??? – A Myf Jun 30 '21 at 03:13
  • this link sir. https://stackoverflow.com/questions/68188566/how-to-convert-binary-to-octal-in-progress-4gl – A Myf Jun 30 '21 at 04:46