Solved

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

Posted on 2008-10-12
6
1,196 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to access ANSI/IEEE Std 754 or equivalent information ? 3 78
delete-remove 14 108
Embarcadero C++ Builder XE10.1 Berlin red arrow Indicator 2 75
Precision Problem in C++ 7 52
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

730 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