Solved

How to replace BYTE buffer with C++, MFC...?

Posted on 2008-10-12
6
1,179 Views
Last Modified: 2013-11-20
I have a BYTE buffer array (Binary data, may be load from a file)
I want to replace any sub-buffer by other sub-buffer
Like String Replace (with CString for example)

It seems, many samples of replacing string to string. But with Buffer of Byte, I have not found yet!

How to do this work most quickly?

Thanks in advanced!
0
Comment
Question by:star6868
6 Comments
 
LVL 44

Expert Comment

by:AndyAinscow
Comment Utility
Step into the CString::Replace function and have a look at how the buffer replacement is implemented there.  Make a copy of the code.  Now modify it to suit your requirements - the logic should be the same.
0
 
LVL 5

Expert Comment

by:isprabu
Comment Utility
star6868,
You can use one of the popular search alogrithms to accomplish your task. "Boyer-Moore algorithm" is one such :http://www-igm.univ-mlv.fr/~lecroq/string/node14.html#SECTION00140
The above link has complete code sample - which you can port to your needs.

Hope this helps!
0
 

Expert Comment

by:kk8
Comment Utility
memcpy()

use BYTE instead of char in this example
http://www.cplusplus.com/reference/clibrary/cstring/memcpy.html
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 250 total points
Comment Utility
you could use std::string::find after converting both buffers to std::string.

   int pos1 = 0;
   int pos2 = 0;
   // you need to pass the size in bytes or any zero byte
   // would terminate the std::string
   std::string buf((char*)bytebuffer, sizbuffer);
   std::string buffind((char*)bytefind, sizfind);
   std::string bufrepl((char*)bytereplace, sizreplace);

   while ((pos2 = buf.find(buffind, pos1)) != std::string::npos)
   {    
          // replace found buffer
          buf    = buf.substr(0, pos2) + bufrepl + buf.substr(pos2+sizfind);
          // next pos is after the replaced string to prevent from infinite loop
          pos1 = pos2+sizreplace;
   }
0
 

Author Comment

by:star6868
Comment Utility
@ itsmeandnobodyelse:
Exactly, I tried your code, it runs well
I'm not familliar with Standar LIBbrary (STD) of C++, I know that's simple if I use well STDLIB!
Thanks!
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
Comment Utility
>>>> I know that's simple if I use well STDLIB!
CString was able to handle binary strings and substrings same as std::string. But unfortunately there is no CString::Find that takes a CString as argument but only a zero-terminated LPCTSTR, what makes it unapplicable for searching binary byte-sequences (even if you were using CString with non-UNICODE settings).  

So you are right, std::string is indeed the better choice for storing and handling binary data.
   while ((pos2 = buf.find(buffind, pos1)) != std::string::npos)

   {     

          // replace found buffer

          buf    = buf.substr(0, pos2) + bufrepl + buf.substr(pos2+sizfind);

          // next pos is after the replaced string to prevent from infinite loop

          pos1 = pos2+sizreplace;

   }

Open in new window

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now