converting to binary code example
Example 1: convert int to binary python
# Python program to convert decimal to binary
# Function to convert Decimal number
# to Binary number
def decimalToBinary(n):
return bin(n).replace("0b", "")
# Driver code
if __name__ ==
print(decimalToBinary(8))
print(decimalToBinary(18))
print(decimalToBinary(7))
Output:
1000
1001
Example 2: decimal to binary
//Java Solution for Decimal To Binary Conversion
import java.util.*;
public class DecimalToBinary {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int dec = sc.nextInt();
StringBuffer sb = new StringBuffer();
while(dec!=0)
{
sb.append(dec%2);
dec=dec/2;
}
System.out.println(sb.reverse());
}
}
Example 3: converting float to binary 16
import struct
#################### bin32 ################################
# import bitstring
# f1 = bitstring.BitArray(float=0.456789, length=32)
# # f1 = bitstring.BitArray(float=0.456789, length=16)
# print(f1.bin)
# print("0011011101001111")
#################### bin32 ################################
def convertFloatToBin16(x):
k = struct.pack(
z =
return str(z)
print(convertFloatToBin16(0.9945678))
Example 4: decimal to binary
std::string binary = std::bitset<8>(n).to_string();