728x90

https://acmicpc.net/problem/11654

 

11654번: 아스키 코드

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

www.acmicpc.net

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

 

 

 

 

 

풀이)

import java.util.Scanner;
import java.util.zip.ZipEntry;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        String s = sc.next();

        System.out.println((int) s.charAt(0));

    }
}

자바가 문자열에서 아스키 코드를 얻는 3가지 방법

💙charAt() 메서드 이용

String str = "Hello";
char firstChar = str.charAt(0);
int asciiValue = (int) firstChar;
System.out.println("아스키 코드: " + asciiValue);

💙getBytes() 메서드 사용

String str = "Hello";
byte[] byteArray = str.getBytes();
int asciiValue = byteArray[0]; // 첫 번째 문자의 아스키 코드
System.out.println("아스키 코드: " + asciiValue);

💙Character 클래스의 getNumericValue() 메서드 사용:

String str = "A";
int asciiValue = Character.getNumericValue(str.charAt(0));
System.out.println("아스키 코드: " + asciiValue);
728x90