Solved

Replace value in QByteArray & QDataStream

Posted on 2011-03-10
6
1,195 Views
Last Modified: 2012-05-11
Hi experts,

I'm trying without success to replace some values in a byte array or in a data stream in QT 4.7

Here's my situation:
I have a binary file with data. Each two bytes of this file represent a signed integer value. I need to replace all the integer values which are 0 with 22 and save the file with the new values.

Any ideas how to proceed ?

Thanks,
Peter
0
Comment
Question by:PeterVarain
  • 2
  • 2
  • 2
6 Comments
 
LVL 16

Expert Comment

by:HooKooDooKu
ID: 35098671
Unless there is an issue with byte order, you should be able to open the file in read/write mode, and call the Read function passing the pointer to a DWORD variable and reading two bytes.  If the variable has a value of 0, seek backwards 2 bytes and write the value 22.

CFile f;
f.Open( strFileName, CFile::modeRead | CFile::modeWrite );
DWORD i;
const DWORD v22 = 22;
while( f.Read( &i, 2 ) == 2 )
{
  if( i == 0 )
  {
    f.Seek( -2, CFile::current );
    f.Write( &v22 );
  }
}

If there is an issue with byte order, then you just need to replace v22=22 with v22=0x1600;
0
 
LVL 16

Expert Comment

by:HooKooDooKu
ID: 35098683
Opps... that should have been something like
f.Write( &v22, 2 )

and you might have to type case the pointers to get something like this to properly compile.
0
 

Author Comment

by:PeterVarain
ID: 35098977
Sorry, I haven't explained very clear what I want to do here...

The file which is saved is not the same file (the source file). I need to move all the data from the source file to a QByteArray and in the process change the 0 values to 22. Then, I need to compress this byte array with qCompress and only then write it to a newly created file.

So, my problem is that I have to move the data from the source file to the byte array and change to zeros...

All the best,
Peter
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 32

Accepted Solution

by:
sarabande earned 500 total points
ID: 35107498
you could read the file using std::ifstream in binary mode:

   std::ifstream infile("xxx.dat", std::ios_base::in | std::ios_base::binary);
   qint16 q16;
   while (infile.read((char*)(&q16), sizeof(q16))
   {
   }

note the qint16 should be 2 bytes what i have taken for granted.

the in loop you could check on 0 and replace by 22. then add to QByteArray

  QByteArray qarr;
  ...
       
        if (q16 == 0)
            q16 = 22;
       qarr.append((char*)(&q16), 2);  

note, the QByteArray would get binary data that way, same as in the file. if you need hexstring conversion you mast do a conversion first.

Sara

0
 
LVL 32

Expert Comment

by:sarabande
ID: 35107504
there is a ) missing at right of while statement.

Sara
0
 

Author Closing Comment

by:PeterVarain
ID: 35114953
Thanks Sara!
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Using 'screen' for session sharing, The Simple Edition Step 1: user starts session with command: screen Step 2: other user (logged in with same user account) connects with command: screen -x Done. Both users are connected to the same CLI sessio…
Setting up Secure Ubuntu server on VMware 1.      Insert the Ubuntu Server distribution CD or attach the ISO of the CD which is in the “Datastore”. Note that it is important to install the x64 edition on servers, not the X86 editions. 2.      Power on th…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…

747 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

12 Experts available now in Live!

Get 1:1 Help Now