c/c++ 字符串string转整数int和浮点数float

本文介绍了一种将特定格式的字符串转换为整数或浮点数的方法,并通过C语言实现了解析器。支持多种字符串类型,包括十六进制、整数和浮点数等。文章详细展示了如何验证字符串的有效性及如何进行转换。

支持的字符串类型有:

十六进制类型,例如:"0x123", "0X123"

整数类型,例如:"1000","-1000"

浮点数类型,例如:"0x789","0X789","1.345","-1.345","1.23e-5", "1.23E-5" ,"-1.23E+5"


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

bool str_to_int(const char* str, const int len, int *result) {
    bool is_hax_x = false;

    for (int index = 0; index < len; index++) {
        if ((str[index] >= '0') && (str[index] <= '9')) {
            continue;
        } else if ((str[index] == '-') || (str[index] == '+')) {
            if (index != 0) {
                return false;
            } else {
                continue;
            }
        } else if ((str[index] == 'x') || (str[index] == 'X')) {
            if (is_hax_x) {
                return false;
            }

            is_hax_x = true;
            if ((index != 1) || (str[index-1] != '0')) {
                return false;
            } else {
                continue;
            }
        } else {
            return false;
        }
    }

    if (is_hax_x) {
        if (1 != sscanf(str, "%x", result)) {
            return false;
        }
    } else {
         if (1 != sscanf(str, "%d", result)) {
            return false;
        }
    }
    
    return true;
}

bool str_to_float(const char* str, const int len, float *result) {
    bool is_has_point = false;

    for (int index = 0; index < len; index++) {
        if ((str[index] >= '0') && (str[index] <= '9')) {
            continue;
        } else if ((str[index] == '-') || (str[index] == '+')) {
            if (index != 0) {
                return false;
            } else {
                continue;
            }
        } else if ((str[index] == 'e') || (str[index] == 'E')) {
            if (!is_has_point) {
                return false;
            }
            int tmp_int = 0;
            if (str_to_int(str+index+1, len-index-1, &tmp_int)) {
                break;
            } else {
                return false;
            }
        } else if (str[index] == '.') {
            if (is_has_point) {
                return false;
            }
            is_has_point = true;
        } else if ((str[index] == 'x') || (str[index] == 'X')) {
            int tmp_int = 0;
            if (str_to_int(str, len, &tmp_int)) {
                break;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    if (1 != sscanf(str, "%f", result)) {
        return false;
    }
    return true;
}

int main() {
    const char *tmp_a = "1.23e-3"; // 1.23e-3  0x778   1.222
    int tmp_int;
    float tmp_float;
    bool is_ok;

    is_ok = str_to_int(tmp_a, strlen(tmp_a), &tmp_int);
    if (is_ok) {
        printf("is int  = %d\n", tmp_int);
    }

    is_ok = str_to_float(tmp_a, strlen(tmp_a), &tmp_float);
    if (is_ok) {
        printf("is float = %f\n", tmp_float);
    }

    return 0;
}

 

C++中,整数浮点数字符串之间相互换有多种API可以实现: ### 整数浮点数字符串 - **`sprintf`**:这是C语言的函数,在C++中也可以使用,它可以将整数浮点数字符串。 ```cpp #include <iostream> int main() { int n = 12345; char strN[50]; sprintf(strN, "%d", n); std::cout << strN << std::endl; float f = 12345.67; char strF[50]; sprintf(strF, "%.2f", f); std::cout << strF << std::endl; return 0; } ``` - **`std::to_string`**:C++标准库提供的函数,可将整数浮点数换为`std::string`类型的字符串。 ```cpp #include <iostream> #include <string> int main() { int a = 1234; float b = 3.14159265; std::string strA = std::to_string(a); std::string strB = std::to_string(b); std::cout << "int to string: " << strA << std::endl; std::cout << "float to string: " << strB << std::endl; return 0; } ``` - **`std::stringstream`**:使用`std::stringstream`类,通过输入输出流的方式进行换。 ```cpp #include <iostream> #include <sstream> #include <string> int main() { int x = 12; double d = 12.34; std::string s; std::stringstream str; str << x; str >> s; std::cout << s << std::endl; str.clear(); str << d; str >> s; std::cout << s << std::endl; return 0; } ``` ### 字符串整数浮点数 - **`stoi`、`stol`、`stoll`**:`stoi`用于将字符串换为`int`类型,`stol`换为`long`类型,`stoll`换为`long long`类型。 ```cpp #include <iostream> #include <string> int main() { std::string str = "1234"; int num = std::stoi(str); std::cout << "string to int: " << num << std::endl; return 0; } ``` - **`stof`、`stod`、`stold`**:`stof`用于将字符串换为`float`类型,`stod`换为`double`类型,`stold`换为`long double`类型。 ```cpp #include <iostream> #include <string> int main() { std::string str = "3.14"; float f = std::stof(str); std::cout << "string to float: " << f << std::endl; return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值