C++ 中 int 转string, 以及10进制转2进制
感谢:http://blog.csdn.net/xiaofei2010/article/details/7434737
以及:http://www.cnblogs.com/nzbbody/p/3504199.html
1. int转string(更多参见:http://www.cnblogs.com/nzbbody/p/3504199.html)
1 #include <iostream> 2 #include <sstream> 3 using namespace std; 4 5 int main() { 6 int aa = 30; 7 stringstream ss; 8 ss<<aa; 9 string s1 = ss.str(); 10 cout<<s1<<endl; // 30 11 12 string s2; 13 ss>>s2; 14 cout<<s2<<endl; // 30 15 16 system("pause"); 17 return 0; 18 }
2.10进制转2进制(更多参见:http://blog.csdn.net/xiaofei2010/article/details/7434737)
1 //十进制转二进制 2 #include<iostream> 3 using namespace std; 4 5 void printbinary(const unsigned int val) 6 { 7 for(int i = 16; i >= 0; i--) { 8 if(val & (1 << i)) 9 cout << "1"; 10 else 11 cout << "0"; 12 } 13 } 14 15 int main() 16 { 17 printbinary(1024); 18 return 0; 19 }
posted on 2015-12-17 16:23 Oliver-cs 阅读(1999) 评论(0) 编辑 收藏 举报