반응형

c 문자 숫자(정수)로 변환(atoi, strtol, strtoul, strtoll, strtoull)

URL : http://blog.naver.com/parkjaeeup/130123294008

 

문자열을 정수로 변환 하는 샘플 입니다.

실수로 변환은 아래 링크를 참고

http://blog.naver.com/parkjaeeup/130126121900


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <errno.h>

int main(int argc, char **argv)
{

char string[256];
char *end;

int base=10;

/*-2147483648~2147483647 at 32bit cpu */
int i=0;

long l=0;

unsigned long lu=0;

long long int ll=0;

unsigned long long int llu=0;


memset(string, 0x00, sizeof(string));
/* 2,147,483,647 */
sprintf(string, "%s", "2147483647");

/* int */

i=atoi(string);
printf("%-22s :%d\n", "int", i);

/* long */

l=strtol(string, &end, base);
printf("%-22s :%li\n", "long", l);

memset(string, 0x00, sizeof(string));
sprintf(string, "%lu", (unsigned long)l*2+1);

/* unsigned long */

lu=strtoul(string, &end, base);
printf("%-22s :%lu\n", "unsigned long", lu);


memset(string, 0x00, sizeof(string));
/* 9,223,372,036,854,775,807 */
sprintf(string, "%s", "9223372036854775807");

/* long long int */

ll=strtoll(string, &end, base);
printf("%-22s :%lli\n", "long long int", ll);

memset(string, 0x00, sizeof(string));
sprintf(string, "%llu", (unsigned long long int)ll*2 + 1);

/* unsigned long long int */
llu=strtoull(string, &end, base);
printf("%-22s :%llu\n", "unsigned long long int", llu);

return 0;
}

참고 :

Technical Reference: Base Operating System and Extensions, Volume 2

strtol, strtoul, strtoll, strtoull, or atoi Subroutine

Purpose

Converts a string to a signed or unsigned long integer or long long integer.

Library

Standard C Library (libc.a)

Syntax

#include <stdlib.h>

long strtol ( String, EndPointer, Base)

const char *String;

char **EndPointer;

int Base;

unsigned long strtoul (String, EndPointer, Base)
const char *String;
char **EndPointer;
int Base;

long long int strtoll (String, EndPointer, Base)
char *String, **EndPointer;
int Base;

unsigned long long int strtoull (String, EndPointer, Base)
char *String, **EndPointer;
int Base;

int atoi (String)
const char *String;

Description

The strtol subroutine returns a long integer whose value is represented by the character string to which the String parameter points. The strtol subroutine scans the string up to the first character
that is inconsistent with the Base parameter. Leading white-space characters are ignored, and an optional sign may precede the digits.

The strtoul subroutine provides the same functions but returns an unsigned long integer.

The strtoll and strtoull subroutines provide the same functions but return long long and unsigned long long integers, respectively.

The atoi subroutine is equivalent to the strtol subroutine where the value of the EndPointer parameter is a null pointer and the Base parameter is a value of 10.

If the value of the EndPointer parameter is not null, then a pointer to the character that ended the scan is stored in EndPointer. If an integer cannot be formed, the value of the EndPointer
parameter is set to that of the String parameter.

If the Base parameter is a value between 2 and 36, the subject sequence's expected form is a sequence of letters and digits representing an integer whose radix is specified by the Base parameter.
This sequence is optionally preceded by a + (positive) or - (negative) sign. Letters from a (or A) to z (or Z) inclusive are ascribed the values 10 to 35; only letters whose ascribed values are less
than that of the Base parameter are permitted. If the Base parameter has a value of 16, the characters 0x or 0X optionally precede the sequence of letters and digits, following the + (positive) or -
(negative) sign if present.

If the value of the Base parameter is 0, the string determines the base. Thus, after an optional leading sign, a leading 0 indicates octal conversion, and a leading 0x or 0X indicates hexadecimal
conversion. The default is to use decimal conversion.

Parameters

String
Points to the character string to be converted.
EndPointer
Points to a character string that contains the first character not converted.
Base
Specifies the base to use for the conversion.

 

c 문자 숫자(실수)로 변환(atof, strtod, strtof, strtold)

 c 문자 숫자(실수)로 변환(atof, strtod, strtof, strtold)

URL : http://blog.naver.com/parkjaeeup/130126121900

 

문자열을 실수로 변환 하는 샘플 입니다.

정수로 변환은 아래 링크를 참고

http://blog.naver.com/parkjaeeup/130123294008

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <errno.h>

int main(int argc, char **argv)
{

char string[256];
char *end;

float f=0.0;
double d=0.0;
long double lf=0.0;

sprintf(string, "%s", "100.0123456789");

/* double */
d=atof(string);

printf("double(atof) : %f\n", d);

d=0.0;

/* double */
d=strtod(string, &end);

printf("double(strtod) : %f\n", d);

/* float */
f=strtof(string, &end);
printf("float(strtof) : %f\n", f);

/* long double */
lf=strtold(string, &end);


printf("long double(strtold) : %Lf\n", lf);

return 0;
}

output :

double(atof) : 100.012346
double(strtod) : 100.012346
float(strtof) : 100.012344
long double(strtold) : 100.012346

 

 

 

c 문자 숫자(실수)로 변환(atof, strtod, strtof, strtold)

반응형
Posted by 공간사랑
,