2016-09-18 4 views
-3

私はこれをインターネットで検索しましたが、正確な解決策を見つけることができませんでした。私が見つけた解決策の1つは文字列として整数を読み取り、charAtメソッド文字をintにキャストしてascii値を出力します。JAVAのintのASCII値を出力する方法

しかし、上記以外の方法でも可能ですか?

int a=sc.nextInt(); 
//code to convert it into its equivalent ASCII value. 

例えば、1として読み取り、整数を考えると、今私はあなたがこれを見ていると仮定49

+0

私たちは、あなたが何をしようとしての明確なアイデアを持っているので、あなたは、実施例を示していないのはなぜ? – shmosel

+2

Java文字は、ASCIIよりも多くの文字を含むUnicodeです –

+0

[javaの文字列をASCII数値に変換]の重複があります(http://stackoverflow.com/questions/16458564/convert-string-to-ascii-numeric-value -in-java) –

答えて

2

でスクリーンに印刷される1のASCII値をしたい:

System.out.print((char)a); 
1

それを行うための簡単な方法は次のとおりです。文字列全体のために


単一文字の
public class ConvertToAscii{ 
    public static void main(String args[]){ 
     String number = "1234"; 
     int []arr = new int[number.length()]; 
     System.out.println("THe asscii value of each character is: "); 
     for(int i=0;i<arr.length;i++){ 
      arr[i] = number.charAt(i); // assign the integer value of character i.e ascii 
      System.out.print(" "+arr[i]); 
     } 
    } 
} 


String number="123"; asciiValue = (int) number.charAt(0)//it coverts the character at 0 position to ascii value

関連する問題