how to convert number to binary using ASCII code example

Example 1: 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 2: int to binary string with 4 characters

Convert.ToString(3, 2).PadLeft(4, '0') // 0011
Convert.ToString(3, 2).PadLeft(8, '0') // 00000011