본문 바로가기
Programming/Java

[Java] - 아스키코드 :: 문자 - 숫자 변환하기

by 구튼탁 2021. 2. 24.
728x90

아스키코드란?

아스키 코드는 미국 ANSI 에서 표준화한 정보교환용 7비트 부호체계이다. 000(0x00)부터 127(0x7F)까지 총 128개의 부호가 사용된다.

이는 영문 키보드로 입력할 수 있는 모든 기호들이 할당되어 있는 부호 체계이며, 매우 단순하고 간단하기 때문에 어느 시스템에서도 적용가능하다는 장점이 있으나, 2바이트 이상의 코드를 표현할 수 없기 때문에 국제표준의 위상은 유니코드에게 넘어갔다.


문자, 숫자 변환하기

import java.util.Scanner;

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

        Scanner scan = new Scanner(System.in);

        char ch = scan.nextLine().charAt(0);
        toInt(ch);

        int num = scan.nextInt();
        toChar(num);
    }
    
	//문자를 숫자로
    static void toChar(int num){
        char ch = (char)num;
        System.out.println(ch);
    }
    
    //숫자를 문자로
    static void toInt(char ch){
        int num = (int)ch;
        System.out.println(num);
    }
}
728x90

댓글