Solved

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

Posted on 2008-10-12
6
1,183 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
ID: 22700376
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
ID: 22700835
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
ID: 22704350
memcpy()

use BYTE instead of char in this example
http://www.cplusplus.com/reference/clibrary/cstring/memcpy.html
0
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 250 total points
ID: 22704475
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
ID: 22719646
@ 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
ID: 22721382
>>>> 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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

809 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