In the development of the following situations often encountered, through some tools captured 16 of data, applied to the code, such as through the Wireshark captured packets, observed program memory data.
However, at development time, this data cannot be used directly, and it needs to be converted as follows in order to be used in code:
I have written a gadget that converts binary data flows into arrays with the following code:
FileNameToArray.cpp:Defines the entry point for the console application.//#include "stdafx.h" #include <stdio.h> #include <iostream>using namespace Std;int Convert (char *szsourcebuf,char *szdestbuf) {char *p =szsourcebuf;while (*p)! = ' + ') {while (*p = = "| | *p = = ' \ t ' | | *p = = ' \ n ') p++;if (*p! = ') {char sztemp[10]={0};sprintf (sztemp, "0x%c%c,", *p,* (p+1)), strcat (szdestbuf,sztemp);p + +; p++;}} return 1;} int _tmain (int argc, _tchar* argv[]) {char szfilename[100] ={"Binaxxry.txt"}; file* f = fopen (szFileName, "+w"); if (f ==null) return 0;char szsourcebuf[1024]={0};int nsrclen = sizeof (SZSOURCEBUF); char szdestbuf[4096] ={0};int NDstLen = sizeof (SZDESTBUF); int npackageindex =0;char szdstfilename[100]={0}; Char *p = STRCHR (szFileName, '. '); *p= ' + '; sprintf (Szdstfilename, "%s_carray.txt", szFileName); FILE *fd =fopen (Szdstfilename, "a"); sprintf (Szdestbuf, "Char szpackage[] ={\n"); fputs (SZDESTBUF,FD); while (1) {memset ( Szsourcebuf,0,nsrclen); memset (Szdestbuf,0,ndstlen); Char*presult= Fgets (szsourcebuf,nsrclen,f), if (PResult! = 0) {Convert (SZSOURCEBUF,SZDESTBUF); Strcat (szdestbuf, "\ n"); Fputs (SZDESTBUF,FD); }elsebreak;} Fclose (f); Fputs ("};", FD); fclose (FD); return 0;}
16 binary data flow to C language array