거북목개발자

[Java] 아스키 코드 ( 문자 ⇆ 숫자) - 백준11654번 본문

Programming Language/Java

[Java] 아스키 코드 ( 문자 ⇆ 숫자) - 백준11654번

거북목개발자 2022. 6. 28. 15:08
728x90

아스키 코드 ( ASCII )

ASCII (American Standard Code for Information Interchange, 미국 정보 교환 표준 부호)

아스키코드표

아스키코드 (문자→숫자)

public static void main(String[] args) {
	Scanner input = new Scanner(System.in);
		
	char userInput = input.nextLine().charAt(0);
	int num = (int)userInput;
		
	System.out.println(num);
}

charAt()은 String으로 저장된 문자열 중 한글자만 선택해 char 타입으로 변환해준다.

charAt()의 ()안에 들어가는 숫자는 문자열에서 문자의 순서를 나타낸다.

 

예를 들어 사용자가 input.nextLine()을 통해 Hello를 입력하는 경우

charAt(0)에 의해 Hello 문자열의 첫 번재 글자 H가 char 타입으로 변환된다.

따라서 num 값인 72가 출력된다.

 

아스키코드 (숫자→문자)

public static void main(String[] args) {
	Scanner input = new Scanner(System.in);
		
	int userInput = input.nextInt();
	char ch = (char)userInput;
		
	System.out.println(ch);
}

기타 : 파이썬 코드

// 문자 -> 정수
print(ord(input()))

// 정수 -> 문자
print(chr(input()))

백준 11654번 : 아스키 코드

 

11654번: 아스키 코드

알파벳 소문자, 대문자, 숫자 0-9중 하나가 주어졌을 때, 주어진 글자의 아스키 코드값을 출력하는 프로그램을 작성하시오.

www.acmicpc.net

728x90
Comments