1 분 소요

#include
#include

#ifdef __what__
정수를 문자열로 변환하는 함수

char *itoa(int value, char *string, int radix);
Integer to Ascii의 준말
첫 번째 인수에 바꾸고자 하는 정수값을 주고 두 번째 인수에 변환한 문자열을 저장할
배열을 준다. 세 번째 인수 radix는 변환시 사용할 진법을 지정

char *ltoa(long value, char *string, int radix);

char *ultoa(unsigned long value, char *string, int radix);
===============================================================================
==========» 이런 위의 정수를 문자열로 변환시켜주는건 없네 그려.. 띠풍.. ㅜㅜ
usr/include/stdlib.h 를 확인해 보면 알수 있다..
gcc에서는 지원을 안하는 함수인것같다. 시간나면 함 짜봐야것다 흐흐
===============================================================================

문자열을 정수로 변환 하는 함수

int atoi(const char *string);

long atol(const char *string); atol 문자열을 10진수로

long strtol(const char *nptr, char **endptr, int base); strtol 문자열을 원하는 진수로

unsigned long strtoul(const char *nptr, char **endptr, int base);

실제로는 itoa와 strtol을 사용해서 테스트 해보겠다.

#endif // __what__

//#define ITOA
// ITOA 선언되어 있지 않다.

#define STRTOL
int main(int argc, char *argv[])
{
#ifdef ITOA
int num=12345;

int radix;

char str[32];

for (radix=2;radix<=20;radix++) {
itoa(num,str,radix);
printf(“%d진법 : %s
“,radix,str);
}
#endif //ITOA

#ifdef TEST
char buffer[20];
int i = 3445;
long l = -344115L;
unsigned long ul = 1234567890UL;

_itoa( i, buffer, 10 );
// i : 문자로 변경하고자 하는 숫자가 들어있는 변수
// buffer : 숫자를 문자로 변경할때 문자가 들어가는 변수
// 10 : 숫자를 어떤 진수 형태로 문자로 변경할것인가를 정의 해주는 값
// 10=> 10진수, 16=> 16진수….

printf( “String of integer %d (radix 10): %s
“, i, buffer );

#endif //TEST

#ifdef STRTOL

char *endptr;

char str[]=”abcd”;

long l=strtol(str,&endptr,16);

printf(“%ld
“,l);
#endif //STRTOL

return 0;
}\

태그:

카테고리:

업데이트: