Python program to convert Base 4 system to binary number Last Updated : 20 Mar, 2023 Comments Improve Suggest changes Like Article Like Report Given a base 4 number N, the task is to write a python program to print its binary equivalent. Conversion Table: Examples: Input : N=11002233 Output : 101000010101111 Explanation : From that conversion table we changed 1 to 01, 2 to 10 ,3 to 11 ,0 to 00.Input : N=321321 Output: 111001111001Method 1: Take an empty string say resultstr.We convert the decimal number to string.Traverse the string and convert each character to an integerIf the integer is 1 or 0 then before converting to binary add '0' to resultstr (Because we cannot have 01,00 in integers)Now convert this integer to binary string and concatenate the resultant binary string to resultstr.Convert resultstr to integer(which removes leading zeros).Return resultstr.Below is the implementation of the above approach as follows: Python3 # function which converts decimal to binary def decToBin(num): # Using default binary conversion functions binary = bin(num) # removing first two character as the # result is always in the form 0bXXXXXXX # by taking characters after index 2 binary = binary[2:] return binary # function to convert base4 to binary def convert(num): # Taking a empty string resultstr = "" # converting number to string numstring = str(num) # Traversing string for i in numstring: # converting this character to integer i = int(i) # if i is 1 or 0 then add '0' to result # string if(i == 1 or i == 0): resultstr = resultstr+'0' # passing this integer to get converted to # binary binary = decToBin(i) # print(binary) # Concatenating this binary string to result # string resultstr = resultstr+binary # Converting resultstr to integer resultstr = int(resultstr) # Return result string return resultstr # Driver code Number = 11002233 # Passing this number to convert function print(convert(Number)) Output: 101000010101111 Time Complexity: O(n), Auxiliary Space: O(1) Here n is the length of the no Method 2: Step-by-step approach: The base4_to_binary function takes a string num representing a number in base 4 as input.The function first converts the number to base 10 by multiplying each digit in the number by 4 raised to the power of its position (i.e., the rightmost digit is in position 0, the next digit is in position 1, and so on). This is done by iterating through the digits of the number in reverse order and adding the product of the digit and 4 raised to the power of its position to a variable base10.The function then converts the number from base 10 to binary using the built-in bin() function in Python. The bin() function returns a string with a '0b' prefix, so we slice off the first two characters to get the binary representation of the number as a string.The function returns the binary representation of the number as a string. Python3 def base4_to_binary(num): # Convert the number to base 10 base10 = 0 for i, digit in enumerate(num[::-1]): base10 += int(digit) * (4 ** i) # Convert the number to binary binary = bin(base10)[2:] return binary num = "11002233" binary = base4_to_binary(num) print(f"The binary representation of {num} is {binary}") # Output: The binary representation of 1032 is 10011010 num = "321321" binary = base4_to_binary(num) print(f"The binary representation of {num} is {binary}") # Output: The binary representation of 30 is 11110 OutputThe binary representation of 11002233 is 101000010101111 The binary representation of 321321 is 111001111001 Time complexity: O(n)Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Python program to convert Base 4 system to binary number V vikkycirus Follow Improve Article Tags : Python Python Programs base-conversion Practice Tags : python Similar Reads Python program to convert binary to ASCII In this article, we are going to see the conversion of Binary to ASCII in the Python programming language. There are multiple approaches by which this conversion can be performed that are illustrated below: Method 1: By using binascii module Binascii helps convert between binary and various ASCII-en 3 min read Python program to convert ASCII to Binary We are having a string s="Hello" we need to convert the string to its ASCII then convert the ASCII to Binary representation so that the output becomes : 01001000 01100101 01101100 01101100 0110111. To convert an ASCII string to binary we use ord() to get the ASCII value of each character and format( 2 min read Python Program to Convert any Positive Real Number to Binary string Given any Real Number greater than or equal to zero that is passed in as float, print binary representation of entered real number. Examples: Input: 123.5 Output: 1 1 1 1 0 1 1 . 1 Input: 0.25 Output: .01 Mathematical Logic along with steps done in programming: Any real number is divided into two pa 4 min read Python Program to Subtract Two Binary Numbers We are given two binary numbers, num1 and num2 and we have to subtract these two binary numbers and return the result. In this article, we will see how we can subtract two binary numbers in Python.Examples:Input: a = "1110" , b = "0110"Output: "1000"Explanation: We can see that when "1110" is subtra 3 min read Python Program to Convert Binary to Hexadecimal Given a binary number, the task is to write a Python program to convert the given binary number into an equivalent hexadecimal number. i.e convert the number with base value 2 to base value 16. In hexadecimal representation we 16 values to represent a number. Numbers 0-9 are expressed by digits 0-9 4 min read Like