Java - Change int to ascii

前端 未结 7 915
迷失自我
迷失自我 2020-12-01 07:32

Is there a way for java to convert int\'s to ascii symbols?

7条回答
  •  旧时难觅i
    2020-12-01 08:06

    In Java, you really want to use Integer.toString to convert an integer to its corresponding String value. If you are dealing with just the digits 0-9, then you could use something like this:

    private static final char[] DIGITS =
        {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
    
    private static char getDigit(int digitValue) {
       assertInRange(digitValue, 0, 9);
       return DIGITS[digitValue];
    }
    

    Or, equivalently:

    private static int ASCII_ZERO = 0x30;
    
    private static char getDigit(int digitValue) {
      assertInRange(digitValue, 0, 9);
      return ((char) (digitValue + ASCII_ZERO));
    }
    

提交回复
热议问题