Write a binary file as an asc hex file

Source: Internet
Author: User

Void hex2asc (byte hex, tchar uchar [3]) <br/> {<br/> // convert 0-f to '0'-'F' <br/> # define bin2txt (x)> 9 )? (X)-10 + _ T ('A'): (x) + _ T ('0 '))) </P> <p> uchar [0] = bin2txt (hex> 4); // Level 4 <br/> uchar [1] = bin2txt (hex & 0x0f ); // four lower places <br/> uchar [2] = 0; // end <br/>}</P> <p> byte asc2hex (const tchar uchar [3]) <br/>{< br/> byte hex; <br/> # define assertishexasc (x) assert (x)> = _ T ('0') & (x) <= _ T ('9 ')) \ <br/> | (x)> = _ T ('A') & (x) <= _ T ('F ')) \ <br/> | (x) >=_ T ('A') & (x) <= _ T ('F '))) <br/> assertishexasc (uchar [0]); // determines whether the data is Valid '0'-'9' A '-'F' <br/> assertishexasc (uchar [1]); // determine whether the data is valid '0'-'9' A '-'f'' A'-'F' <br/> # define txt2bin (X) (x)> = _ T ('0') & (x) <= _ T ('9 '))? (X)-_ T ('0'): \ <br/> (x) >=_ T ('A') & (X) <= _ T ('F '))? (X)-_ T ('A') + 10): (x)-_ T ('A') + 10 ))) <br/> hex = (txt2bin (uchar [0]) <4; // Level 4 <br/> hex | = (txt2bin (uchar [1]); // four lower bits <br/> return hex; <br/>}</P> <p> ulonglong convbin2txt (lpctstr pszsrcfile, lpctstr pszdstfile, int nperlinebytecount = 16) <br/>{< br/> ulonglong ncount = 0; // conversion counter <br/> byte nbin, ntxt [2]; // convert buff <br/> byte nnewline [] = {'/R','/N '}; // press enter to wrap the line </P> <p> try <br/> {<br/> cfile srcfi Le (pszsrcfile, cfile: moderead); // open the source file <br/> cfile dstfile (pszdstfile, cfile: modecreate | cfile: modereadwrite ); // open the target file </P> <p> while (srcfile. read (& nbin, 1) = 1) // read 1 byte <br/>{< br/> # define bin2txt (x)> 9 )? (X) + 'a'-10): (x) + '0 ')) // convert 0-f to '0'-'F' <br/> ntxt [0] = bin2txt (nbin> 4 ); ntxt [1] = bin2txt (nbin & 0x0f); // 4-bit conversion </P> <p> dstfile. write (ntxt, 2); // write the target file <br/> ncount ++; // accumulate the converted bytes </P> <p> If (ncount % nperlinebytecount = 0) // The number of bytes that do not exist <br/> dstfile. write (nnewline, 2); // write a carriage return line break <br/>}</P> <p> srcfile. close (); // close the source file </P> <p> nbin = 0; <br/> dstfile. write (& nbin, 1); // write 0 end text <br/> dstfile. close (); // close the target file </P> <p >}< br/> catch (cfileexception * E) // capture exceptions <br/>{< br/> E-> reporterror (); // report exceptions <br/> E-> Delete (); // clear the exception <br/>}</P> <p> return ncount; // return the number of converted bytes <br/>}</P> <p> // test code <br/> convbin2txt (_ T ("C: // XXXX. PCM "), _ T (" C: // 1.txt"); </P> <p>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.