convert int to binary code example
Example 1: int to binary java
String binary = Integer.toBinaryString(num);
Example 2: integer to binary java
Integer.toString(100,8)
Integer.toString(100,2)
Integer.toString(100,16)
Example 3: decimal to binary
import java.util.*;
public class DecimalToBinary {
public static void main(String[] args) {
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 4: decimal to binary
std::string binary = std::bitset<8>(n).to_string();
Example 5: decimal to binary
#include <iostream>
#include <stdlib.h>
int main ()
{
int i;
char buffer [33];
printf ("Enter a number: ");
scanf ("%d",&i);
itoa (i,buffer,10);
printf ("decimal: %s\n",buffer);
itoa (i,buffer,16);
printf ("hexadecimal: %s\n",buffer);
itoa (i,buffer,2);
printf ("binary: %s\n",buffer);
return 0;
}
Example 6: binary to decimal
= (11001010)2
= 1x27+1x26+0x25+0x24+1x23+0x22+1x21+0x20
= 128+64+0+0+8+0+2+0
= (202)10