중급 81. 문자열을 실수로 변환하기 1 (atof)

학습 내용 : 소수점이 포함된 문자열을 실수값으로 변환하는 방법을 이해합니다. 소스 코드 : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include #include void main() { char *string1 = "3.141592... 원주율"; char *string2 = "원주율 3.141592..."; float tempFloat1 = -1; float tempFloat2 = -1; puts(string1); puts(string2); tempFloat1 = atof(string1); tempFloat2 = atof(string2); printf("%f\n", tempFloat1); printf("%f\n", tempFloat2); } 실행 화면 :

중급 80. 문자열을 정수로 변환하기 4 (strtoul)

학습 내용 : 컴퓨터가 이해할 수 있는 수는 0과 1뿐입니다. 0과 1을 사용하는 것을 2진법이라고 하며, 2진수로 표현된 문자열을 사람이 사용하는 10진수로 변환하는 방법을 학습합니다. 소스 코드 : 1 2 3 4 5 6 7 8 9 10 11 12 13 #include #include void main() { char *string1 = "10011"; int radix = 2; long tempLong = -1; tempLong = strtoul(string1, NULL, radix); printf("2진수 %s의 10진수 값은 %d입니다.\n", string1, tempLong); } 실행 화면 :

중급 79. 문자열을 정수로 변환하기 3 (strtol)

학습 내용 : 컴퓨터는 16진수 문자열을 많이 사용하기 때문에, 16진수 문자열을 사람이 사용하는 10진수로 변환하는 방법을 학습합니다. 소스 코드 : 1 2 3 4 5 6 7 8 9 10 11 12 13 #include #include void main() { char *string1 = "0xa"; // 16진수를 표현하기 위해서는 0x를 붙여야함 int radix = 16; // 변환할 진수 long tempLong = -1; tempLong = strtol(string1, NULL, radix); printf("16진수 %s의 10진수 값은 %d입니다.\n", string1, tempLong); } 실행 화면 :