字符串转换问题
怎么把字符串转换成2进制代码?例如吧A转换成1000100,要求对汉字也可以哦。而且是一大串字符转换。本人使用的是vc++6.0编译器。
#include <string> #include <iostream> using namespace std; int main() { string str; char ch; cin >> str; string::iterator iter = str.begin(); while (iter != str.end()) { ch = *iter++; for (int i = 0; i < 8; ++i) cout << ((ch >> i) & 1); } }
string::iterator iter = str.begin(); while (iter != str.end()) { ch = *iter++; for (int i = 0; i < 8; ++i) cout << ((ch >> i) & 1); }这部分能不能给些注释?
#include <string> #include <iostream> using namespace std; int main() { string str; FILE *fp; char ch; if((fp=fopen("file.txt","wb+"))==NULL) { cout<<"can not open this file.\n"; return 0; } cin >> str; string::iterator iter = str.begin(); while (iter != str.end()) { ch = *iter++; for (int i = 0; i < 8; ++i) fputc(((ch >> i) & 1),fp); } fclose(fp); }要怎么改才能吧那些2进制码写到想要的文件中呢?