You are on page 1of 6

Java Convert Decimal to Binary

We can convert decimal to binary in java using Integer.toBinaryString() method or


custom logic.

Decimal to Binary : Integer.toBinaryString()


The Integer.toBinaryString() method converts decimal to binary string. The signature
of toBinaryString() method is given below:

1. public static String toBinaryString(int decimal)

Let's see the simple example of converting decimal to binary in java.

1. public class DecimalToBinaryExample1{


2. public static void main(String args[]){
3. System.out.println(Integer.toBinaryString(10));
4. System.out.println(Integer.toBinaryString(21));
5. System.out.println(Integer.toBinaryString(31));
6. }}

Run by: java DecimalToBinaryExample1

1010

10101

11111

Java String to char Example: charAt() method


Let's see the simple code to convert String to char in java using charAt() method.

1. tring s="hello";
2. char c=s.charAt(0);//returns h

Let's see the simple example of converting String to char in java.

1. public class StringToCharExample1{


2. public static void main(String args[]){
3. String s="hello";
4. char c=s.charAt(0);//returns h
5. System.out.println("1st character is: "+c);
6. }}

Test it Now

Output:

1st character is: h

Java String length() method example


FileName: LengthExample.java

1. public class LengthExample{


2. public static void main(String args[]){
3. String s1="javatpoint";
4. String s2="python";
5. System.out.println("string length is: "+s1.length());//10 is the length of
javatpoint string
6. System.out.println("string length is: "+s2.length());//6 is the length of python
string
7. }}

Test it Now

Output:

string length is: 10


string length is: 6

Java Program to Print the ASCII Value

ASCII is an acronym that stands for American Standard Code for Information
Interchange. In ASCII, a specific numerical value is given to different
characters and symbols, for computers to store and manipulate, and while
storing and manipulating the electronic device always works with the binary
value of the ASCII number given. As it is impossible to do that in the original
form.

Approaches: There are several ways to print ASCII value or code of a


specific character which are listed below briefing the concept followed by a
java example for the implementation part.

1. Using brute force Method


2. Using the type-casting Method

Method 1: Assigning a Variable to the int Variable

In order to find the ASCII value of a character, simply assign the character to a
new variable of integer type. Java automatically stores the ASCII value of that
character inside the new variable.
Implementation: Brute force Method

Java

// Java program to print ASCII Value of


Character
// by assigning variable to integer

public class GFG {

// Main driver method


public static void main(String[] args)
{
// Character whose ASCII is to be
computed
char ch = '}';

// Creating a new variable of type int


// and assigning the character value.
int ascii = ch;

/* Java stores the ascii value there


itself*/

// Printing the ASCII value of above


character
System.out.println("The ASCII value of
" + ch
+ " is: " + ascii);
}
}

Output

The ASCII value of } is: 125


Method 2: Using Type-Casting

Type-casting in java is a way to cast a variable into another datatype which


means holding a value of another datatype occupying lesser bytes. In this
approach, a character is a typecast of type char to the type int while printing,
and it will print the ASCII value of the character.

Java

// Java program to print ASCII Value of


Character
// using type-casting

// Importing java generic libraries


import java.util.*;

public class GFG {

// Main driver method


public static void main(String[] args)
{
// Character whose ASCII is to be
computed
char ch = '}';

// Typecasting the character to int


and
// printing the same
System.out.println("The ASCII value
of " + ch
+ " is: " +
(int)ch);
}
}

Output
The ASCII value of } is: 125

You might also like