Advertisement
dooly386

bintoascii, asciitobin file

Jun 6th, 2019
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.88 KB | None | 0 0
  1. void BinToAscFile(const std::string &binfile,const std::string &txtfile)
  2. {
  3.  
  4.     {
  5.         std::ifstream infile( binfile, std::ios::binary );
  6.         std::ofstream outfile(txtfile);
  7.         unsigned char v ;
  8.  
  9.         char temp[4];
  10.         ZeroMemory(temp,4);
  11.         while( infile.read( reinterpret_cast<char*>(&v), sizeof(v) ) )
  12.         {
  13.             sprintf(temp,"%02x",v);
  14.             outfile << temp << ' ' ;
  15.         }
  16.         outfile << '\n' ;
  17.  
  18.     }
  19. }
  20. void AscToBinFile(const std::string &txtfile,const std::string &binfile)
  21. {
  22.     {
  23.         std::vector<unsigned char> seq ;
  24.         {
  25.             std::ifstream file(txtfile) ;
  26.             char temp[4];
  27.             while( !file.eof())
  28.             {
  29.                 file >> temp;
  30.                 unsigned char c = strtol(temp,NULL,16);
  31.                 seq.push_back((unsigned char)c) ;
  32.             }
  33.  
  34.         }
  35.  
  36.         {
  37.             std::ofstream file( binfile, std::ios::binary ) ;
  38.             for( unsigned char v : seq ) file.write( reinterpret_cast<const char*>(&v), sizeof(v) ) ;
  39.         }
  40.  
  41.  
  42.     }
  43.  
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement