I try to converting bytes with 8 digits into ASCII code for example if user enter "0000111" the program convert it to it's ASCII code and shows 'p'(the value of character c in the code below became 'p')

public static void main(String[] args) {
        Byte bytes;
        char c;
        Scanner input=new Scanner(System.in);
        System.out.println("enter your specefic bytes(maximkum 8 digits to show the ASCII code) : ");
        bytes=input.nextByte();
        c=(char)bytes;
        System.out.println("the Equivalent ASCII code of your bytes is = "+c);
    }

Can you accept a String then convert the characters to ASCII?
They can still be digits or they can be any char (if that's allowed)

Example:

import java.util.Scanner;

public class StringToAsciiCodes
{
   public static void main(String[] args)
   {

      Scanner input=new Scanner(System.in);
      System.out.print("Enter your specefic bytes(maximum 8 digits to show the ASCII code): ");
      String strInput=input.next();
      char c =' ';
      for(int i=0; i < strInput.length(); i++)
      {
         c = strInput.charAt(i);
         System.out.println(c + " = " + (0+c));
      }
   }
}

Edited 4 Years Ago by thines01: clarity

That code was to get you started, it wasn't intended to be a complete solution. Nobody here will just give you the complete solution. You will find that it's very useful if you understand what it is doing, and how that differs from your use of nextByte().

Comments
Widsom

That example shows you something you might have missed about converting a char (or byte) to an ascii value.

As JamesCherrill just said, it was to get you started.

It was not intended to upset you.

That example shows you something you might have missed about converting a char (or byte) to an ascii value.

As JamesCherrill just said, it was to get you started.

It was not intended to upset you.

Tnx mr thines.i didn't mean you upset me.now I have one question about your code.the problem is how can I cast int to char.for example if I enter "0000111" it gives me the number 49.how can I cast it to char that shows 'c' character.i mean a work code version of below code:

Int b=(0+c);
Char n = (char) b

;

i have tried this code but it shows the number 1.what is wrong with it???

public static void main(String[] args) {
        Scanner input=new Scanner(System.in);
        System.out.print("Enter your specefic bytes(maximum 8 digits to show the ASCII code): ");
        String strInput=input.next();
        char c =' ';
        for(int i=0; i < strInput.length(); i++)
        {
            c = strInput.charAt(i);
            System.out.println("the element of "+i+" th "+c);

        }
        System.out.println(c + " = " + (0+c));

        System.out.println((0+c));
        int b=(0+c);
        char n=(char)b;
        System.out.println("the character is "+n);
        
    }

my inputs and output:

Enter your specefic bytes(maximum 8 digits to show the ASCII code): 0000111
the element of 0 th 0
the element of 1 th 0
the element of 2 th 0
the element of 3 th 0
the element of 4 th 1
the element of 5 th 1
the element of 6 th 1
1 = 49
49
the character is 1

it has to show character c instead of 1

it has to show character c

To see the binary value of a char you can do this:

System.out.println("c=" + Integer.toBinaryString('c') + " " + (int)'c');  // c=1100011  99
Comments
thanks for notification that c equals 1100011 in ACII code system

To see the binary value of a char you can do this:

Java Syntax (Toggle Plain Text)
System.out.println("c=" + Integer.toBinaryString('c') + " " + (int)'c'); // c=1100011 99

you know i want the inverse of this mechaanism none of the upside codes aren't good.i just want to get ASCII code(in Binary mode) from user then convert it to character.for example when user enter 1100011 then program converts it to the c.plz help in efficient way.

i have tried this code but it shows the number 1.what is wrong with it???

Do you understand about binary numbers? Each digit represents a different power of 2. Your code inputs eight binary digits, then just uses the last one and ignores all the others. (The last one is '1', so that's what you see). You need to use all 8 digits to get their total value. Keep trying.

This article has been dead for over six months. Start a new discussion instead.