tags:

views:

43

answers:

3

i need java codes. pls help me. example. when i enter the number in ASCII; 0 the output will be nul 1 for soh 2 for stx until it reaches the max number of ascii. please i need it now. thanks in advance ;)

pls consider this code.. this code output is a ascii number. how can i reverse it?

String test = "ABCD";
 for ( int i = 0; i < test.length(); ++i ) {
   char c = test.charAt( i );
   int j = (int) c;
   System.out.println(j);
    }
A: 

Just cast an integer value to char.:

int value = (int) 'a';
System.out.println((char) value);  // prints a

If you need some literal output for ASCII values below '0', you'll need a mapping from the integer value (the ASCII number) to the literal, like this:

String[] literals0to32 = {"NUL", "SOH", "STX", /* to be continued */ };

private static String toLiteral(int value) {

   if (value < 0 || value > 255)
      throw new IKnowThatIHaveToValidateParametersException();

   if (value < 32) 
     return literals0To32[value];
   else
     return (char) value;
}
Andreas_D
thanks, but how about when my prof. enter the number 0 it'll print nul.?
jhoanne
No, in that case it will print something undefined. Editing my answer for a quick solution (fragment) for ASCII
Andreas_D
can you edit again by putting all the codes starts in public class. please.
jhoanne
@jhoanne - of course ... not. ;) You've already accepted another answer and I already provided too much help for something that smells like homework. I'm not creating a copy'n'paste solution.
Andreas_D
sorry for that ;( thanks for giving me your info. it really helps me. but it is not my homework. sorry again.
jhoanne
+1  A: 
import java.io.*;
import java.lang.*;
    public class CharToASCII{
        public static void main(String args[]) throws IOException{
          BufferedReader buff = new BufferedReader(new InputStreamReader(System.in));
          System.out.println("Enter the char:");
          String str = buff.readLine();
          for ( int i = 0; i < str.length(); ++i ){
            char c = str.charAt(i);
            int j = (int) c;// your work is done here
            System.out.println("ASCII OF "+c +" = " + j + ".");
            }
        }
      }
org.life.java
i copied your prog. but it has 4 errors.
jhoanne
@jhoanne you need to make proper imports
org.life.java
@jhoanne updated the code
org.life.java
oh. sorry. ;) i forgot to wrote that. thanks a lot
jhoanne
can you reverse the output of this code? instead of enter the char: can you i use enter the ascii code. e.g. (Enter ascii code: 0 char of 0 is NUL
jhoanne
@jhoanne ya for that you need to convert `int` to `char`
org.life.java
ok ill try thanks a lot ;)
jhoanne
instead of enter the char: can you i use enter the ascii code. e.g. (Enter ascii code: 41, char of 41 is ) because i can convert int to char. pls help again
jhoanne
@jhoanne yes you can , I would suggest you to clear fundamentals , It would be trouble for you in future if you just follow this approch
org.life.java
ok thaks alot again. it helps me a lot.
jhoanne
A: 

You can print the corresponding Unicode Control Pictures, e.g. \u2400 for ␀ (nul).

trashgod