查看字符串的二进制形式

来源:互联网 发布:手机贴纸软件 编辑:程序博客网 时间:2024/05/22 04:40

不介绍了,刚才码了这么多字,被丢失了,我晕。主要是在MFC中读取文件的时候,书上都是以文本形式的方式读取,但是有时候想看看数据流的二进制长什么样,以前没有做过,不知道MFC中怎么查看二进制流,所以就试着将得到的文本数据转换成二进制流的形式查看。代码中用到了STL的栈容器,第一次在实际代码中需要的时候用STL,以前感觉学了他就是为了面试的,一直没有排上用处,这次感觉挺好,终于觉得没有白学。

#include "stdafx.h"#include <iostream.h>#include <string.h>#include <stack>using namespace std;void dectobinary(unsigned int num)//十进制转换成二进制{stack<unsigned int> bin;//为了取余的数倒叙成二进制,我使用了STL的栈容器     while(num!=0) { bin.push(num%2); num=num/2; } while(!bin.empty()) { cout<<bin.top(); bin.pop(); } cout<<'\0';//每个字符间空格}void strtobinary(const char * str)//取出字符串中的字符{   int len=strlen(str);   char *s=(char *)str;   for (int j=0;j<len;j++)   {   dectobinary(s[j]);   }}int main(int argc, char* argv[]){char *str="woaini";//要查看的字符串strtobinary(str);cout<<endl;return 0;}


原创粉丝点击