Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2008-10-12
6
Medium Priority
?
1,221 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 45

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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 1000 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

Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seveā€¦
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilationā€¦
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.
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.

597 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