当前位置:网站首页>C language data type replacement

C language data type replacement

2022-07-05 08:29:00 Sharing master

Use C Standard library functions

The specific approach is to first string Convert to char* character string , Then it is converted to the desired numerical type through the corresponding type conversion function . Need to include standard library functions <stdlib.h>
(1)string Convert to int32_t

1

2

3

4

5

string love="77";

int ilove=atoi(love.c_str());

// perhaps 16 Bit platform is converted to long int

int ilove=strtol(love.c_str(),NULL,10);

  (2)string Convert to uint32_t

1

2

3

4

5

6

7

8

9

//str: String to be converted

//endptr: Point to str The first non numeric character after a number in

//base: Conversion base ( Base number ), Range from 2 to 36

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

# Example

string love="77";

unsigned long ul;

ul = strtoul(love.c_str(), NULL, 10);

  (3)string Convert to uint64_t

1

2

string love="77";

long long llLove=atoll(love.c_str());

  (4)string Convert to uint64_t

1

2

3

4

5

6

unsigned long long int strtoull (const char* str, char** endptr, int base);

# Example

string love="77";

unsigned long long ull;

ull = strtoull (love.c_str(), NULL, 0);

  (5)string Convert to float or double

1

2

3

string love="77.77";

float fLove=atof(love.c_str());

double dLove=atof(love.c_str());

  (6)string Convert to long double

1

long double strtold (const char* str, char** endptr);

原网站

版权声明
本文为[Sharing master]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202140544213495.html