美文网首页
c/c++中把数字变为字符串

c/c++中把数字变为字符串

作者: Joe_HUST | 来源:发表于2017-09-12 18:28 被阅读0次

int 转化为 string

  1. 使用sprintf().[在C和C++中均可用]
// int sprintf( char *buffer, const char *format, [ argument] … );
 // buffer:char型指针,指向将要写入的字符串的缓冲区。
 // format:格式化字符串。
 // [argument]...:可选参数,可以是任何类型的数据。
 // 返回值:字符串长度(strlen)
 int aa = 30;
 char c[8]; 
 int length = sprintf(c, "%05X", aa); 
 cout<<c<<endl; 

  1. 使用stringstream
int aa = 30;
 stringstream ss;
 string s2;
 ss<<aa; 
 ss>>s2;
 cout<<s2<<endl; // 30
或者
 s2 = ss.str();
 cout<<s2<<endl; // 30

C++引入了ostringstream、istringstream、stringstream这三个类,要使用他们创建对象就必须包含#include <sstream>头文件


string转化为int

  1. 使用strtol(string to long)
string s = "17";
 char* end;
 int i = static_cast<int>(strtol(s.c_str(),&end,16));
 cout<<i<<endl; // 23
 i = static_cast<int>(strtol(s.c_str(),&end,10));
 cout<<i<<endl; // 17

static_cast一般用来将枚举类型转换成整型或者整型转换成浮点型。也可以用来将指向父类的指针转换成指向子类的指针。
做这些转换前,你必须确定要转换的数据确实是目标类型的数据,因为static_cast不做运行时的类型检查以保证转换的安全性。
也因此,static_cast不如dynamic_cast安全。
对含有二义性的指针,dynamic_cast会转换失败,而static_cast却直接且粗暴地进行转换。这是非常危险的。例如:

class B {};
class D : public B {};
void f(B* pb, D* pd) {
   D* pd2 = static_cast<D*>(pb);   // Not safe, D can have fields  and methods that are not in B.
   B* pb2 = static_cast<B*>(pd);   // Safe conversion, D always contains all of B.
}
  • static_cast和dynamic_cast都可以用于类层次结构中基类和子类之间指针或引用的转换。所不同的是,static_cast仅仅是依靠类型转换语句中提供的信息(尖括号中的类型)来进行转换;而dynamic_cast则会遍历整个类的继承体系进行类型检查。比如:
class B {
public:
   virtual void Test(){}
};
class D : public B {};
void f(B* pb) {
   D* pd1 = dynamic_cast<D*>(pb);
   D* pd2 = static_cast<D*>(pb);
}
  1. 如果pb确实是指向一个D类型的对象,那pd1和pd2的值是相同的,即使pb为NULL。
  2. 如果pb实际指向的是一个B类型的对象,那dynamic_cast就会转换失败,并返回NULL(此时pd1为NULL);而static_cast却依据程序员指定的类型简单地返回一个指针指向假定的D类型的对象(此时pd2不为NULL),这当然是错误的。

  1. 使用sscanf()
 int i;
 sscanf("17","%D",&i);
 cout<<i<<endl; // 17
 sscanf("17","%X",&i);
 cout<<i<<endl; // 23
 sscanf("0X17","%X",&i);
 cout<<i<<endl; // 23
  1. 使用stringstream
 string s = "17";
 stringstream ss;
 ss<<s;
 int i;
 ss>>i;
 cout<<i<<endl; // 17
  1. 使用boost库中的lexical_cast
string s = "17";
int i = boost::lexical_cast<int>(s); 
cout<<i<<endl; // 17

相关文章

  • c/c++中把数字变为字符串

    int 转化为 string 使用sprintf().[在C和C++中均可用] 使用stringstream C+...

  • PAT甲级题目合集

    1001 C++新特性里的to_string()函数可以将数字变为字符串,非常方便 1002 映射法 1003 经...

  • C++字符串与数字的转换

    字符串转数字:以下函数属于C++,需包含 数字转字符串:

  • C_C++ 字符串数字的转换

    C++ 字符串流 stringstream C++ stream library 中的 stringstream ...

  • C语言学习笔记

    C/C++格式化字符串说明 C++的格式化字符串经常用作格式化数字的输出、字符串合并和转换等等很多场合。 1. 格...

  • C++<第十九篇>:字符串

    C++ 中的字符串有两种形式:(1)C风格的字符串 (2)C++引入的 string 类型的字符串。 (1)C风格...

  • C++字符串处理小结

    C++中的字符串类型 常用的C++的字符串类型主要是std::string。它是模板std::basic_stri...

  • 字符串

    C++提供了两种字符串的表示形式: C风格字符 C++引入的string类型 C风格 函数 C++中的String类

  • atoi()和stoi()的区别

    (1)相同点 都是C++的字符处理函数,把数字字符串转换成int输出 头文件都是#include...

  • NDK开发错误 use of invalid jobject 0

    JNI中直接返回C/C++的字符串时会报如下错误 需要将C/C++中的字符串转换为中间层jstring返回 Ndk...

网友评论

      本文标题:c/c++中把数字变为字符串

      本文链接:https://www.haomeiwen.com/subject/xtocsxtx.html