Results 1 to 8 of 8

Thread: Add QByteArray

  1. #1
    Join Date
    Mar 2012
    Posts
    13
    Thanks
    1
    Qt products
    Qt4

    Default Add QByteArray

    I have two binary files, of which I have no clue about their data organization. I have them as QByteArray stream and overlap them meaning a[i]=b[i]+c[i].

    I made an attempt with

    QByteArray x,y,z
    for(k=0;k<x.size();k++){
    for(l=0;l<y.size();l++){
    if(k=l){
    z[k]=x[k]+y[l];
    z.append(z[k]);
    }
    }
    }

    I know that it was a naive attempt but couldn't do more as I have no clue about the internal data organization, all I know is they are binary. Any suggestions or help please !!
    Last edited by CodeHunt; 1st May 2012 at 21:50. Reason: Typo

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Add QByteArray

    Your attempt uses two loops where one is required and contains a number of coding errors that stop it even compiling. If X.size() == Y.size() then:
    Qt Code:
    1. QByteArray x(10, 0x0A);
    2. QByteArray y(10, 0x0B);
    3.  
    4. Q_ASSERT(x.size() == y.size());
    5. for (int i = 0; i < x.size(); ++i)
    6. z.append( x.at(i) + y.at(i) );
    7. qDebug() << x.toHex();
    8. qDebug() << y.toHex();
    9. qDebug() << z.toHex();
    To copy to clipboard, switch view to plain text mode 


    Really though, your problem is potentially more difficult. You have two binary blobs (X & Y) of indeterminate length and structure and (for whatever reason) you want to byte-wise add them to get a third binary blob. Here are the obvious questions:
    • What is the desired behaviour if X & Y are different lengths?
    • What is the desired behaviour if the sum of bytes overflows a byte?
    • Are you after the sum or the exclusive-or of the bytes?
    • Are you trying to implement an encryption algorithm?

  3. #3
    Join Date
    Mar 2012
    Posts
    13
    Thanks
    1
    Qt products
    Qt4

    Default Re: Add QByteArray

    X and Y will be of same length always. If there is an overflow, then it should be rounded to the nearest decimal. I don't expect it to overflow as I am sure that the array size will be the same and therefore the values.I am concerned about the sum.Its not encryption algorithms, but deals with images largely.

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Add QByteArray

    Bytes are integers: there's no such beast as "rounded to the nearest decimal". You will never get fractional parts.

    Overflowing has nothing to do with the size of the arrays. It is about the size of the elements in the array. If byte A has the value 130 and byte B the value 134 then the sum, 264, cannot fit in a byte, i.e. it overflows the byte which can only hold a number in the range 0 to 255.
    Qt Code:
    1. unsigned char a = 130;
    2. unsigned char b = 134;
    3. unsigned char c = a + b;
    4. qDebug() << c;
    5. // prints 8 not 264
    To copy to clipboard, switch view to plain text mode 

    If you are manipulating images then you really should look at the capability of QImage and QPainter

  5. #5
    Join Date
    Mar 2012
    Posts
    13
    Thanks
    1
    Qt products
    Qt4

    Default Re: Add QByteArray

    I understand that, as I know that all the value in my program range between 0-127 ( if any negative values encountered,absolute values are considered). I am give two binary files which are written by the same program and I know the values of data in the files. And I also know that those binary files are extracted from an image and they will of size in terms of pixels. Any ideas on how can I operate on the values so that I could get the output similar to what I get on placing one image exactly over the other.

  6. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Add QByteArray

    There's more than one way to place an image over another. The result may be only the image on top, or you only see the image underneath where the top image had transparent pixels, or you see a blended version of both images regardless of transparency etc.

    It's not generally possible without knowing the precise nature of the bytes. Are they raw pixel values or are they some compressed or well-known format? Is the image colour or monochrome? Are the colour channels red/green/blue or some other colour model? Do the images have an alpha channel?

    Another thought:
    I understand that, as I know that all the value in my program range between 0-127 ( if any negative values encountered,absolute values are considered).
    A signed byte can hold numbers in the range -128 to 127. The absolute value of the negative extreme can still cause overflow issues if you add two of them.

  7. #7
    Join Date
    Mar 2012
    Posts
    13
    Thanks
    1
    Qt products
    Qt4

    Default Re: Add QByteArray

    I am writing the binary files using following method.
    //....
    typedef std::basic_ofstream<unsigned char, std::char_traits<unsigned char> > uofstream;
    uofstream op(filename, ios::out | ios::binary);
    op.write(size, 5);
    op.write(RGrBl, width*height*3);
    op.write(a,width*height*difference);/difference is int size[4]
    op.close();


    I want to add the values at each BYTE in the files created by above function. I am unable to write it because of exception at run time saying bad memory allocation, and referring to mlock.c

    I just want to add the hex values at all the bytes but I am getting exceptions at time saying bad memory allocation and referring to mlock.c. I am using

    //x and y are read from files created using the above
    QByteArray alpha; for(int k=0;k<qMin(x.size(), y.size());k++){ alpha.append(x[k]-y[k]); } qDebug()<<z.toHex();
    QByteArray beta; for(int j=0;j<qMin(x.size(),alpha.size());++j) { alpha.append(x[j]+z[j]); } qDebug()<<beta.toHex();

    Where do you think I am going wrong ?
    Thanks
    Last edited by CodeHunt; 3rd May 2012 at 13:00. Reason: Code Highlighting

  8. #8
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Add QByteArray

    Where do you think I am going wrong ?
    You should use [code]...[/code] tags around your code

    mlock.c is not part of Qt. Run your code in your debugger until it crashes and then look at the backtrace to find the offending part of your code.

    This code:
    Qt Code:
    1. //x and y are read from files created using the above
    2. QByteArray alpha; for(int k=0;k<qMin(x.size(), y.size());k++){ alpha.append(x[k]-y[k]); } qDebug()<<z.toHex();
    3. QByteArray beta; for(int j=0;j<qMin(x.size(),alpha.size());++j) { alpha.append(x[j]+z[j]); } qDebug()<<beta.toHex();
    To copy to clipboard, switch view to plain text mode 
    You add byte differences to an array called alpha then print an array called z.
    You then add byte sums to the array called alpha then print the array called beta.
    Neither debug output is likely to be what you expect.

    Also, you second loop is bounds are set based x and alpha size but you are using them to index an unrelated z[] which is probably empty or, if not, smaller than either x or alpha. Out-of-bounds memory accesses are a recipe for program termination.

Similar Threads

  1. Replies: 1
    Last Post: 22nd June 2011, 08:12
  2. XoR on hex/QByteArray
    By meena in forum Qt Programming
    Replies: 1
    Last Post: 13th July 2010, 15:41
  3. Replies: 9
    Last Post: 25th July 2009, 13:27
  4. QByteArray
    By gyre in forum Newbie
    Replies: 4
    Last Post: 9th October 2007, 18:30
  5. QByteArray in Qt3
    By joseph in forum Qt Programming
    Replies: 1
    Last Post: 6th September 2007, 06:16

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.