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
Solved

Replace value in QByteArray & QDataStream

Posted on 2011-03-10
6
1,286 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
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 
LVL 33

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 33

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension (http://www.experts-exchange.com/discussions/210281/Attachments-with-no-extension.html). This reminded me of questions tha…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

808 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