16 binary data flow to C language array

Source: Internet
Author: User

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

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.