Programa para Convertir Número Octal a Número Binario

Dado un número octal como entrada, la tarea es convertir ese número en un número binario.
Ejemplos: 
 

Input : Octal = 345
Output : Binary = 011100101
Explanation : 
Equivalent binary value of 3: 011
Equivalent binary value of 4: 100
Equivalent binary value of 5: 101

Input : Octal = 120
Output : Binary = 001010000

Número octal : un número octal es un sistema de numeración posicional con una raíz o base de 8 y utiliza ocho símbolos distintos.
Número binario : Un número binario es un número expresado en el sistema numérico binario de base 2, que utiliza solo dos símbolos: 0 (cero) y 1 (uno).
 

Para convertir un número octal a binario, el equivalente binario de cada dígito del número octal se evalúa y se combina al final para obtener el número binario equivalente. 
 

A continuación se muestra la implementación del enfoque anterior: 
 

C++

// C++ program to convert
// Octal number to Binary
 
#include <iostream>
using namespace std;
 
// Function to convert an
// Octal to Binary Number
string OctToBin(string octnum)
{
    long int i = 0;
 
    string binary = "";
 
    while (octnum[i]) {
        switch (octnum[i]) {
        case '0':
            binary += "000";
            break;
        case '1':
            binary += "001";
            break;
        case '2':
            binary += "010";
            break;
        case '3':
            binary += "011";
            break;
        case '4':
            binary += "100";
            break;
        case '5':
            binary += "101";
            break;
        case '6':
            binary += "110";
            break;
        case '7':
            binary += "111";
            break;
        default:
            cout << "\nInvalid Octal Digit "
                 << octnum[i];
            break;
        }
        i++;
    }
 
    return binary;
}
 
// Driver code
int main()
{
    // Get the Hexadecimal number
    string octnum = "345";
 
    // Convert Octal to Binary
    cout << "Equivalent Binary Value = "
         << OctToBin(octnum);
 
    return 0;
}

Java

// Java program to convert
// Octal number to Binary
import java.util.*;
class Solution
{
  
// Function to convert an
// Octal to Binary Number
static String OctToBin(String octnum)
{
    long i = 0;
  
    String binary = "";
     
    while (i<octnum.length()) {
         
        char c=octnum.charAt((int)i);
        switch (c) {
        case '0':
            binary += "000";
            break;
        case '1':
            binary += "001";
            break;
        case '2':
            binary += "010";
            break;
        case '3':
            binary += "011";
            break;
        case '4':
            binary += "100";
            break;
        case '5':
            binary += "101";
            break;
        case '6':
            binary += "110";
            break;
        case '7':
            binary += "111";
            break;
        default:
            System.out.println( "\nInvalid Octal Digit "+ octnum.charAt((int)i));
            break;
        }
        i++;
    }
  
    return binary;
}
  
// Driver code
public static void main(String args[])
{
    // Get the Hexadecimal number
    String octnum = "345";
  
    // Convert Octal to Binary
    System.out.println( "Equivalent Binary Value = "+ OctToBin(octnum));
  
}
 
}
//contributed by Arnab Kundu

Python3

# Python3 program to convert
# Octal number to Binary
 
# defining a function that returns
# binary equivalent of the number
def OctToBin(octnum):
     
    binary = "" # initialising bin as String
     
    # While loop to extract each digit
    while octnum != 0:
         
        # extracting each digit
        d = int(octnum % 10)
        if d == 0:
             
            # concatenation of string using join function
            binary = "".join(["000", binary])
        elif d == 1:
             
            # concatenation of string using join function
            binary = "".join(["001", binary])
        elif d == 2:
             
            # concatenation of string using join function
            binary = "".join(["010", binary])
        elif d == 3:
             
            # concatenation of string using join function
            binary = "".join(["011", binary])
        elif d == 4:
             
            # concatenation of string using join function
            binary = "".join(["100", binary])
        elif d == 5:
             
            # concatenation of string using join function
            binary = "".join(["101", binary])
        elif d == 6:
             
            # concatenation of string using join function
            binary = "".join(["110",binary])
        elif d == 7:
             
            # concatenation of string using join function
            binary = "".join(["111", binary])
        else:
             
            # an option for invalid input
            binary = "Invalid Octal Digit"
            break
 
        # updating the oct for while loop
        octnum = int(octnum / 10)
         
    # returning the string binary that stores
    # binary equivalent of the number
    return binary
 
# Driver Code
octnum = 345
 
# value of function stored final_bin
final_bin = "" + OctToBin(octnum)
 
# result is printed
print("Equivalent Binary Value =", final_bin)
 
# This code is contributed by Animesh_Gupta

C#

// C# program to convert Octal number to Binary
 
class GFG
{
 
// Function to convert an
// Octal to Binary Number
static string OctToBin(string octnum)
{
    int i = 0;
 
    string binary = "";
     
    while (i < octnum.Length)
    {
         
        char c = octnum[i];
        switch (c)
        {
        case '0':
            binary += "000";
            break;
        case '1':
            binary += "001";
            break;
        case '2':
            binary += "010";
            break;
        case '3':
            binary += "011";
            break;
        case '4':
            binary += "100";
            break;
        case '5':
            binary += "101";
            break;
        case '6':
            binary += "110";
            break;
        case '7':
            binary += "111";
            break;
        default:
            System.Console.WriteLine( "\nInvalid Octal Digit "+
                                                    octnum[i]);
            break;
        }
        i++;
    }
 
    return binary;
}
 
// Driver code
static void Main()
{
    // Get the Hexadecimal number
    string octnum = "345";
 
    // Convert Octal to Binary
    System.Console.WriteLine("Equivalent Binary Value = " +
                                         OctToBin(octnum));
}
}
 
// This code is contributed by mits

PHP

<?php
// PHP program to convert
// Octal number to Binary
 
// Function to convert an
// Octal to Binary Number
function OctToBin($octnum)
{
    $i = 0;
 
    $binary = (string)"";
 
    while ($i != strlen($octnum))
    {
        switch ($octnum[$i])
        {
        case '0':
            $binary.= "000";
            break;
        case '1':
            $binary .= "001";
            break;
        case '2':
            $binary .= "010";
            break;
        case '3':
            $binary .= "011";
            break;
        case '4':
            $binary .= "100";
            break;
        case '5':
            $binary .= "101";
            break;
        case '6':
            $binary .= "110";
            break;
        case '7':
            $binary .= "111";
            break;
        default:
            echo("\nInvalid Octal Digit ".
                             $octnum[$i]);
            break;
        }
        $i++;
    }
    return $binary;
}
 
// Driver code
 
// Get the Hexadecimal number
$octnum = "345";
 
/// Convert Octal to Binary
echo("Equivalent Binary Value = " .
                OctToBin($octnum));
 
// This code is contributed
// by PrinciRaj1992

Javascript

<script>
 
// JavaScript program to convert
// Octal number to Binary
 
// Function to convert an
// Octal to Binary Number
function OctToBin(octnum)
{
    let i = 0;
   
    let binary = "";
      
    while (i<octnum.length) {
          
        let c=octnum[i];
        switch (c) {
        case '0':
            binary += "000";
            break;
        case '1':
            binary += "001";
            break;
        case '2':
            binary += "010";
            break;
        case '3':
            binary += "011";
            break;
        case '4':
            binary += "100";
            break;
        case '5':
            binary += "101";
            break;
        case '6':
            binary += "110";
            break;
        case '7':
            binary += "111";
            break;
        default:
            document.write( "<br>Invalid Octal Digit "+ octnum[i]);
            break;
        }
        i++;
    }
   
    return binary;
}
 
// Driver code
// Get the Hexadecimal number
let octnum = "345";
 
// Convert Octal to Binary
document.write( "Equivalent Binary Value = "+ OctToBin(octnum));
 
// This code is contributed by avanitrachhadiya2155
 
</script>
Producción: 

Equivalent Binary Value = 011100101

 

Publicación traducida automáticamente

Artículo escrito por Kanishk_Verma y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *