Results 1 to 7 of 7

Thread: QByteArray data manuplation.

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Nov 2010
    Posts
    100
    Thanks
    38
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4

    Default QByteArray data manuplation.

    hello frenz, i am having a small problem i am not able to figure out how to remove first 12 Chars from a QByteArray and append the next 40 chars into another byte array or string.
    something like i have to remove the first 6 bytes 03b702200001 and then append the next 20 bytes 00de00de00de00de00de00de00de00de00de00de00de00de00 de00de00de00de00de00de00de00de and so on to the next line of data also i tried using .remove(); but i cud't get it right.

    Qt Code:
    1. 03b702200001 00de00de00de00de00de00de00de00de00de00de00de00de00de00de00de00de00de00de00de00de
    2. 03b702200002 00de00de00de00de00de00de00de00de00de00de00de00de00de00de00de00de00de00de00de00de
    3. 03b702200003 00de00de00de00de00de00de00de00de00de00de00de00de00de00de00de00de00de00de00de00de
    4. 03b702200004 00 de00de00de00de00de00de00de00de00de00de00de00de00de00de00de00de00de00de00de00de
    To copy to clipboard, switch view to plain text mode 

    i am getting continuous data from rs232 terminal which i am pushing to a QByteArray this is what i did to remove the first 12 characters and append the next 40 characters .
    Qt Code:
    1. for(int i=12; i< datstr.size(); i++)
    2. {
    3. newdat.append(datstr.at(i));
    4. bytecount++
    5.  
    6. if(bytecount==40)
    7. {
    8. newdat.remove(0,12);
    9. bytecount=0;
    10. }
    11. }
    12. textBrowser->append(newdat);
    13. datstr.clear();
    To copy to clipboard, switch view to plain text mode 
    i am not able to think about a exact logic to do this.
    Last edited by nagabathula; 1st December 2010 at 16:26.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QByteArray data manuplation.

    Putting binary data into a text edit box is not going to end up with anything useful so either your data is textual and not binary or you are going to get weird result in the text browser. One way or the other you do not seem to control what you are doing.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    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: QByteArray data manuplation.

    What's wrong with QByteArray::mid() or QString::mid()?

    I think you are confusing yourself by talking about a series of bytes in an array one moment and the representation of those bytes as a string in hexadecimal the next. They are different beasts and require different handling.

    If the data is raw bytes in a QByteArray then you want 20 bytes starting a byte 6. If the data is a hex string in either a QByteArray or a QString then you want 40 characters starting at character 12.

    As an aside, each of your example hexadecimal strings represents 46 bytes of raw data as 92 hex digits (spaces ignored). You seem to indicate you are expecting to get all the bytes other than the first 6 but are asking for only 20.

  4. #4
    Join Date
    Nov 2010
    Posts
    100
    Thanks
    38
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4

    Default Re: QByteArray data manuplation.

    Hello thank you for replying,
    i am actually pushingback that data from the sqlite db where i have saved all the data after aligning and bitshifting.. Now i need to remove the frame id in order to save only the channel data. the frame id is 6 bytes in size 03b702200001 after which 20 bytes is data after which is 6 bytes of frame id again i have to remove those 6 bytes and save the next 20 bytes this cycle goes on, i could't use QStringPattern to remove the frameID cause the frame id increments after every 20 bytes so the string patter would change. i need to think about a logic to remove 6 bytes and save 20 bytes remove the next 6 and save 20 bytes again and so on.. Which i am not able to figure out i tried looking for a similar example but din find.
    I actually though evey thing wud be in Bytes in QByteArray but its taking for example "1d" as 2 chars i think then a single byte. thats why i got a lil confused with it,.

    @wysota I am appending the data into a text Browser to just check the out put. i would be removing the text Browser later and save all that data into a db. tats all temperature data i will equate the data and display it in QTableView.

    thank you

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QByteArray data manuplation.

    Quote Originally Posted by nagabathula View Post
    @wysota I am appending the data into a text Browser to just check the out put. i would be removing the text Browser later and save all that data into a db. tats all temperature data i will equate the data and display it in QTableView.
    That's not the point. You can't append binary data to a widget that expects text so you either don't have text or you don't have binary, there is no automatic conversion between the two. One of your two usages of the data is bound to fail.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. The following user says thank you to wysota for this useful post:

    nagabathula (4th December 2010)

  7. #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: QByteArray data manuplation.

    Quote Originally Posted by nagabathula View Post
    Hello thank you for replying,
    i am actually pushingback that data from the sqlite db where i have saved all the data after aligning and bitshifting.. Now i need to remove the frame id in order to save only the channel data. the frame id is 6 bytes in size 03b702200001 after which 20 bytes is data after which is 6 bytes of frame id again
    No it isn't if your example data is anything to go by. Expressed as a hexadecimal string with bytes numbered:
    Qt Code:
    1. // 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3
    2. // 0 1 2 3 4 5 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9
    3. -- 03b702200001 00de00de00de00de00de00de00de00de00de00de00de00de00de00de00de00de00de00de00de00de
    4. -- 03b702200002 00de00de00de00de00de00de00de00de00de00de00de00de00de00de00de00de00de00de00de00de
    To copy to clipboard, switch view to plain text mode 
    There are 40 bytes after the 6-byte header.

    i have to remove those 6 bytes and save the next 20 bytes this cycle goes on, i could't use QStringPattern to remove the frameID cause the frame id increments after every 20 bytes so the string patter would change.
    Nobody suggested you use "QStringPattern", primarily because it does not exist in Qt.

    i need to think about a logic to remove 6 bytes and save 20 bytes remove the next 6 and save 20 bytes again and so on.. Which i am not able to figure out i tried looking for a similar example but din find.
    You certainly do need to think about the logic. You don't need to remove anything in order to access a subsection from the middle.
    I actually though evey thing wud be in Bytes in QByteArray but its taking for example "1d" as 2 chars i think then a single byte. thats why i got a lil confused with it,.
    Everything in a QByteArray is a byte. The string "1d" is two characters, and in a QByteArray each ASCII character is represented by a byte. You are going to really struggle with this low-level byte manipulation until you grasp the difference between the value in a byte (a binary number) and the representation of that value in human-readable form (whether hexadecimal, octal, decimal, or base64). This is not the place to learn that.

    Answer these questions for yourself:
    What are you working on: an array of the actual bytes, or a hexadecimal string representing the bytes?
    What are you trying to obtain: an array of the actual desired bytes or a hexadecimal string representing the bytes?
    How many bytes, or hexadecimal characters if using a string, are there?
    How can the functions already pointed out in this thread access parts of the byte array (or string)?

  8. The following user says thank you to ChrisW67 for this useful post:

    nagabathula (4th December 2010)

  9. #7
    Join Date
    Nov 2010
    Posts
    100
    Thanks
    38
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4

    Default Re: QByteArray data manuplation.

    Hello ChrisW i solved my problem. this is what i did to remove the frame id and append only the required data. I checked the final result on a textBrowser it shows exactly what i expected.

    Qt Code:
    1. for(int i=0; i< datstr.size(); i++)
    2. {
    3. newdat.append(datstr.mid(posi, 80)); //intial value of posi =12;
    4. posi+=92;
    5. }
    6. textBrowser_2->append(newdat);
    To copy to clipboard, switch view to plain text mode 

    Thank you so much you made me think and solve my problem myself i read about QByteArray::mid as you suggested me before in this thread, and came up with this one.

    Thank you again
    Last edited by nagabathula; 4th December 2010 at 15:20.

Similar Threads

  1. Replies: 3
    Last Post: 29th April 2010, 19:11
  2. inver data from QByteArray
    By aj2903 in forum Qt Programming
    Replies: 2
    Last Post: 16th January 2010, 07:56
  3. Replies: 9
    Last Post: 25th July 2009, 13:27
  4. Corrupt JPEG data: premature end of data segment
    By node_ex in forum Qt Programming
    Replies: 1
    Last Post: 19th August 2008, 08:57
  5. QByteArray with network data
    By merlvingian in forum Qt Programming
    Replies: 1
    Last Post: 1st June 2007, 17:53

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
  •  
Qt is a trademark of The Qt Company.