2

I have to write a Java program for cryptography which takes a number from the user and adds( or subtracts the number if it is negative) from the ASCII code of the character. For example if the number taken is 2, and the given string is "ABCZ" it should return "CDEB". I've figured out most of it, but I'm not sure how to return the character which has a given ASCII code. So if the ASCII is 65,how do I return "A"? I'm only in class 10,so please keep it as simple as possible. Thanks in advance.

aqua
  • 719
  • 4
  • 11
  • 17
  • 3
    possible duplicate : http://stackoverflow.com/questions/7693994/how-to-convert-ascii-code-0-255-to-the-associated-character – ritesh May 22 '13 at 07:37
  • 2
    Good on you, aqua! Start early and you'll be a gun programmer already before you begin college. – vikingsteve May 22 '13 at 07:45

1 Answers1

1

All you need is to cast the number to a character.

int yourInt = 65;
char ch = (char) yourInt;
System.out.println(yourInt);
System.out.println(ch);
// Output:
// 65
// A
Mike de Dood
  • 393
  • 1
  • 10