string 和经常使用类型之间的转换

一、int 类型转换成string类型
std::string CommString::int_to_str(const int value)
{
char tmp[128] = {};ide

snprintf(tmp, sizeof(tmp), "%d", value);   //转换成char

        return std::string(tmp);                           //强制类型转换成 string
    }

二、double 类型转换成string类型
std::string CommString::double_to_str(const double value)
{
char tmp[128] = {};code

snprintf(tmp, sizeof(tmp), "%lf", value);

        return std::string(tmp);
    }

三、char类型转换成string
std::string CommString::char_to_str(const char value)
{
char tmp[10] = {};string

snprintf(tmp, sizeof(tmp), "%c", value);

        return std::string(tmp);
    }

四、string类型转换成int
int CommString::str_to_int(const std::string &str)
{
int ivalue = 0;it

sscanf(str.c_str(), "%d", &ivalue);

        return ivalue;
    }

五、char 类型转换成int类型
int CommString::str_to_int(const char
sz)
{
int ivalue = 0;class

sscanf(sz, "%d", &ivalue);

        return ivalue;
    }

六、 char * 类型转换成double类型
double CommString::str_to_double(const std::string &str)
{
double value = 0.0;类型转换

sscanf(str.c_str(), "%lf", &value);

        return value;
    }
相关文章
相关标签/搜索