二进制问题
底层的程序员一定会遇到二进制的问题
#include <iostream> #include <string> #include <sstream> using namespace std; //整数转换为string类型 string int2str(int n){ stringstream ss; string s; ss<<n; ss>>s; return s; } //将十进制整数转换为二进制 string int2b(int a){ int b = a%2;//余数 a = a/2;//除数 string str = int2str(b); while(a!=0){ b = a%2; a = a/2; str = int2str(b) + str; } return str; } int main(){ char str[32],*p; p=itoa(34,str,16); cout<<p<<endl; cout<<str<<endl; cout<<int2b(34)<<endl;
getchar(); return 0; }结果为