linux----读取文件汉字并输出计算哈希值

 main.cpp+                                                                                                                                                                                                                                                                                                                                                         buffers 
  1 #include <iostream>
  2 #include <unistd.h>
  3 #include <sys/types.h>
  4 #include <fcntl.h>
  5 #include <map>
  6 #include <string>
  7 #include <cstring>
  8 #include <algorithm>
  9 
 10 using namespace std;
 11 
 12 string intToA(int ,int);
 13 
 14 int main(int argc,char *argv[])
 15 {
 16     map<string,string>hanzi_map;
 17 
 18     int ofd ;
 19 
 20     char buf[3];
 21 
 22     ofd = open(argv[1],O_RDWR);
 23 
 24     int len = read(ofd,buf,sizeof(buf));
 25 
 26    // write(STDOUT_FILENO,buf,len);
 27 
 28  //cout<<endl;
 29     size_t fv;
 30     size_t sv;
 31 
 32    // cout<<buf<<endl;
 33 
 34     while(len>1){//这个while条件可以防止最后出现的是乱码;
 35      //   cout<<buf<<endl;
 36      //   cout<<len<<endl;
 37         cout<<buf[0]<<buf[1]<<buf[2]<<endl;
 38         //必须这样才能输出一个汉字;
 39         fv = ((unsigned char)buf[0]-129)*190;
 40         sv = ((unsigned char)buf[1]-64);
 41         size_t cnt = fv+sv-(unsigned char)buf[1]/128;
 42         string res;
 43         res = intToA(cnt,2);
 44         hanzi_map.insert(make_pair(buf,res));
 45         cout<<res<<endl;
 46      //   write(STDOUT_FILENO,buf,len);
 47 
 48         cout<<endl;
 49         len=read(ofd,buf,sizeof(buf));
 50     }
 51     cout<<"二的value"<<hanzi_map["二"]<<endl; 
 52     cout<<"123"<<endl;
 53     std::cout << "Hello world" << std::endl;                                                                                                                                                                                                                                                                                                                               
 54     return 0;
 55 }
 56 
 57 
 58 string intToA(int n,int radix)    //n是待转数字,radix是指定的进制
 59 {
 60     string ans="";
 61     do{
 62         int t=n%radix;
 63         if(t>=0&&t<=9)  ans+=t+'0';
 64         else ans+=t-10+'a';
 65         n/=radix;
 66     }while(n!=0);   //使用do{}while()以防止输入为0的情况
 67     reverse(ans.begin(),ans.end());
 68     return ans; 
 69 }
 70 
 71 
 72 //增加的工作:加权
~                                                                                                                                                                                                                                                                                                                                                                              
~                            

最近实习,工作需要,研究了一天终于搞清楚。

发布了158 篇原创文章 · 获赞 37 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/XUCHEN1230/article/details/87992532