개발 일기

숫자세기 문제 본문

Tech/Algorithm

숫자세기 문제

flow123 2021. 12. 26. 11:41

 

 

		for (int j = 0; j < 10; j++) {
			num[j] = (char) j;
			System.out.println(num);
		}

num[1] = (char) 1 //즉'1'이 되는 줄 알았다. 

그런데 char의 1의 numeric value 는 1이 아니다. 48이다. 

그렇기 때문에 int를 char 로 바꿔줄 때는, 아스키 코드에 맞춰서 변환해줘야한다.

'0'은 48을 가지기 때문에, char(48) = '0' 이 된다. 

 

		for (int j = 0; j < 10; j++) {
			num[j] = (char) ('0' + j);
			System.out.println(num);
		}

 

https://stackoverflow.com/questions/53096607/java-problem-with-casting-an-int-to-char

 

Java - Problem with casting an int to char

I have a bit of confusion regarding casting from int to char data type, this is what i have; int k = 3; System.out.println((char)k + " " + k) The output should have been 3 3 yet, i got this ins...

stackoverflow.com

 

이재욱님의 ascii 표 

 

https://gocoder.tistory.com/1891

Comments