Results 1 to 15 of 15

Thread: Bitwise shifting and ORing HexaDecimal Value

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

    Default Bitwise shifting and ORing HexaDecimal Value

    Hello Every One,
    I am stuck with a problem with bitwise shifting and ORing of 2 bytes.. Actually i am getting data on RS232 port in ascii which i convert into hex and save the converted data into the db.

    Qt Code:
    1. data = (QByteArray::fromRawData(buff,numBytes)).toHex();
    To copy to clipboard, switch view to plain text mode 

    now i need to bitshift left and OR each byte from the db to get the valid data. This is the first time i am doing some thing like this. So i am confused don't know how to go about it.

    This the Hexa Decimal data which i have saved into the database . Here i need to Bitwise shift left the first byte "1d" by 5 times , Next i need to bitwise shift left the second byte "17" and OR the second and the first byte. so on for all the consecutive data.

    1d17110009008f008f008f008f008f008f008f008f008f008f 008f008f008f008f008f008f008f008f008f008f
    1d1711000a008f008f008f008f008f008f008f008f008f008f 008f008f008f008f008f008f008f008f008f008f
    1d1711000b008f008f008f008f008f008f008f008f008f008f 008f008f008f008f008f008f008f008f008f008f
    1d1711000c008f008f008f008f008f008f008f008f008f008f 008f008f008f008f008f008f008f008f008f008f

    Qt Code:
    1. QSqlQuery sq1("select ChannelData from Bulkdb where rowid = (" + QString::number(rowcnt) + ")");
    2. if(sq1.exec())
    3. {
    4. if (sq1.first())
    5. {
    6. do
    7. {
    8. rowdata.push_back(sq1.value(0).toString());
    9. } while(sq1.next());
    10. }
    11. }
    12.  
    13. for(int j = 0; j < rowdata.count(); j++)
    14. databuff.append(rowdata.at(j));
    15.  
    16. for(int i= 0;i<databuff.count() ;i++)
    17. {
    18. databuff1.append(databuff.at(i));
    19. alignbuff = ((databuff1.at(ld) << 5) // I am trying to Bitwise shift left each byte here , not sure if what i am doing is correct .
    20. final_data = secondByte OR | firstByte;
    21. // Not able to figure out a logic
    22. }
    To copy to clipboard, switch view to plain text mode 

    Not able to figure out a logic to OR the bytes like.. 2nd Byte|1st Byte, 4th Byte|3rd Byte , 6th Byte|5 byte , 8th byte|7th Byte and so on for all the data present.

    some one Please who has an example or a suggestion it will help me a lot.

    Thank You

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Bitwise shifting and ORing HexaDecimal Value

    If you want the hexadecimal data, you do not use toHex(). This is for displaying the data, not using it.

    You can use the [] operator.

    Then you can use the normal bitwise operators on it.
    Shifting 5 bits is done like this: << 5

  3. The following user says thank you to tbscope for this useful post:

    nagabathula (14th November 2010)

  4. #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: Bitwise shifting and ORing HexaDecimal Value

    I really don't know why you want to try bitwise operations on a string of characters, which is what a the hexadecimal representaion is, when you already have the raw bytes. Something like this seems more sensible:
    Qt Code:
    1. QByteArray data = QByteArray::fromRawData(buff,numBytes);
    2. QByteArray result;
    3. for (i = 0; i< data.size(); i += 2) {
    4. result.append( ((data.at(i) << 5) | (data.at(i+1)) );
    5. }
    To copy to clipboard, switch view to plain text mode 

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

    nagabathula (14th November 2010)

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

    Default Re: Bitwise shifting and ORing HexaDecimal Value

    Thank You so much ChrisW. What you have suggested makes more sense. I din't know how to go about it before. I will implement what you have given now. I was breaking my head over this problem for hours. Thank you a million.

    Regards

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

    Question Re: Bitwise shifting and ORing HexaDecimal Value

    Hello ChrisW i have a small doubt from the code you suggested.
    for (i = 0; i< data.size(); i += 2)
    I din exactly understand how this for loop works what does " i+=2 " do in this loop.

    This is the part of code in my program where i am trying to insert the data after the bitwise shift and ORing is done, But i am getting an error at Sqlite db insert query. Should i convert QByteArray to QString to insert into the db., how do i convert it.
    Qt Code:
    1. if(port->bytesAvailable())
    2. {
    3. numBytes = port->bytesAvailable();
    4. if(numBytes > sizeof(buff))
    5. numBytes = sizeof(buff);
    6. i = port->read(buff,numBytes);
    7. QByteArray data = QByteArray::fromRawData(buff,numBytes);
    8. QByteArray result;
    9.  
    10. for (i = 0; i< data.size(); i += 2)
    11. {
    12. result.append(((data.at(i) << 5) | (data.at(i+1))));
    13.  
    14. }
    15. QString sq("INSERT INTO Bulkdb values(%1,%2)");
    16. sq = sq.arg(rowcount)
    17. .arg("'"+ result +"'"); //error C2668: 'QString::arg' : ambiguous call to overloaded function
    18. m_query.exec(sq);
    19. rowcount++;
    20. textBrowser->append(result);
    21. }
    To copy to clipboard, switch view to plain text mode 

    I am getting an error when i compile this code ChrisW. How do i insert QByteArray data into Sqlite DB..
    error C2668: 'QString::arg' : ambiguous call to overloaded function

  8. #6
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Bitwise shifting and ORing HexaDecimal Value

    'i += 2' mean 'add 2 to the value of i'.

    Your string construction is failing because 'result' isn't a string; it's a byte array. You need to convert it to a string before performing string manupulations on it.

  9. The following user says thank you to SixDegrees for this useful post:

    nagabathula (15th November 2010)

  10. #7
    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: Bitwise shifting and ORing HexaDecimal Value

    Given the sample data you showed us, the bytes in your result don't look like a particularly meaningful ASCII or Latin1 string. The first two bytes are 0x1d and 0x17 and you do this:
    Qt Code:
    1. 0x1d = 0001 1101
    2. Shift << 5 = 1010 0000 (Some bits lost out left end)
    3. 0x17 = 0001 0111
    4. ORed = 1011 0111 (0xB7, an interpunct in ISO 8859-1 (Latin1) and not valid ASCII)
    To copy to clipboard, switch view to plain text mode 
    So, you probably want to store the data as a Base64, Hex or percent-encoded string, or directly in a BLOB column.
    QByteArray::toBase64(),
    QByteArray::toHex(),
    QByteArray::toPercentEncoding()

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

    Default Re: Bitwise shifting and ORing HexaDecimal Value

    Hello ChrisW ,

    I have to actually use a 16 bit int variable, how should i do that how sud i assign the QByteArray to be 16 bits.?
    You were right it is not in ascii format, i am converting it into Hex.
    Qt Code:
    1. QByteArray data = (QByteArray::fromRawData(buff,numBytes)).toHex();
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. 0x1d = 0000 0000 0001 1101
    2. Shift << 5 = 0000 0011 1010 0000
    3. 0x17 = 0000 0000 0001 0111
    4. ORed = 0000 0011 1011 0111
    To copy to clipboard, switch view to plain text mode 



    thank you
    Last edited by nagabathula; 15th November 2010 at 14:17.

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

    Default Re: Bitwise shifting and ORing HexaDecimal Value

    Hello i think i figured out what the problem is.. I am getting the ORed hex data but i am missing the first byte, i think it is taking only 8 bits so "03" is missing i am getting only "b7". How do i get a parameter as QByteArray and retrun a quint16 value.

    Qt Code:
    1. 0x1d = 0000 0000 0001 1101
    2. Shift << 5 = 0000 0011 1010 0000
    3. 0x17 = 0000 0000 0001 0111
    4.  
    5. ORed = 0000 0011 1011 0111
    6. ----------------------------------------
    7. 0 3 b 7
    To copy to clipboard, switch view to plain text mode 
    But i am getting only b7 i think its because QByteArray is taking it as 8 bits only. How can i assign 16 bits to get the actual aligned data. ??

    This is what i am doing in the test code.
    Qt Code:
    1. QByteArray result;
    2. QByteArray ba,da;
    3.  
    4. ba[0] = 0x1d;
    5. ba[1] = 0x17;
    6. ba[2] = 0x11;
    7. ba[3] = 0x00;
    8. ba[4] = 0x00;
    9. // quint16 result = qFromBigEndian<quint16>((uchar*)result.data());
    10.  
    11. for (int i = 0; i < ba.size(); i+=2)
    12. {
    13. result.append(((ba.at(i) << 5)|(ba.at(i+1))));
    14.  
    15. }
    16. da.append(result.toHex());
    17. textBrowser->append(da);
    To copy to clipboard, switch view to plain text mode 

    How can i make QByteArray ba as a 16 bit. ?

    thank you
    Last edited by nagabathula; 15th November 2010 at 14:15.

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

    Default Convert QByteArray to quint 16

    Hello friends,

    How do i make a QByteArray value to be stored in a 16 bit int to align the data. ..
    I looked a lot every wer but not finding an example to do it right.

    thank you

  14. #11
    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: Convert QByteArray to quint 16

    A QByteArray stores bytes. It doesn't store anything else. Look at QVector or QList for storage of other types.

    If you need to process two bytes into a single 16-bit int then you do this:
    Qt Code:
    1. quint16 r = static_cast<unsigned char>(a) << 5 | static_cast<unsigned char>(b);
    To copy to clipboard, switch view to plain text mode 
    The casts are there to avoid sign-extension if the most-significant bit of the bytes are set (i.e. they are negative)

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

    nagabathula (18th November 2010)

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

    Default Re: Convert QByteArray to quint 16

    Hello ChrisW thank you so much for taking time and helping me out with my problem.

    I have a doubt i actually wanted Each byte in the QByteArray to be of 16-bits size.?
    I think now each byte is QByteArray is of 8-bits. So i am loosing the data,
    only the first 8 bits are taken so i am loosing the other 8 bits. I am getting only "b7" . If i have a 16-bit size for each byte then i will get the right data.
    Qt Code:
    1. ORed = 0000 0011 1011 0111
    2. 0 3 b 7
    To copy to clipboard, switch view to plain text mode 
    This is how i tried your code but i din't get any value appended to the Text Browser.
    Is this the right way to do it.
    Qt Code:
    1. QByteArray result;
    2. ba[0] = 0x1d;
    3. ba[1] = 0x17;
    4. ba[2] = 0x11;
    5. ba[3] = 0x00;
    6. for (int i = 0; i < ba.size(); i+=2)
    7. {
    8. //result.append(((ba.at(i) << 5)|(ba.at(i+1))));
    9. quint16 result=((static_cast<unsigned int>(ba.at(i) << 5))|(static_cast<unsigned int>(ba.at(i+1))));
    10.  
    11. }
    12. da.append(result.toHex());
    13. textBrowser->append(da);
    To copy to clipboard, switch view to plain text mode 

    If i have each byte in QByteArray ba; as 16-Bits size. i think my problem will be solved.How do you think i should do it Chris. ?

    regards
    Last edited by nagabathula; 17th November 2010 at 09:01.

  17. #13
    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: Convert QByteArray to quint 16

    Your code will output nothing because it never puts anything in the result QByteArray (compare line 10 and 11). Even if it did your result would not be what you expect.

    A QByteArray stores bytes, nothing else. As in my last post you want to look at the QVector or QList templates to store other types.

    Qt Code:
    1. ByteArray ba; // an array of bytes (chars actually)
    2. ba[0] = 0x1d;
    3. ba[1] = 0x17;
    4. ba[2] = 0x11;
    5. ba[3] = 0x00;
    6.  
    7. QList<quint16> result; // A list of 16 bit unsigned integers
    8.  
    9. for (int i = 0; i < ba.size(); i+=2)
    10. result.append(
    11. (static_cast<unsigned char>(ba.at(i) << 5))
    12. | static_cast<unsigned char>(ba.at(i+1))
    13. );
    14. }
    To copy to clipboard, switch view to plain text mode 
    You have to convert result to hex entry by entry, there is no toHex() to rely on. If you don't need the result retained in binary form (i.e. the result QList) then you can build the hex string as you go along. Conversion

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

    nagabathula (19th November 2010)

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

    Thumbs up Re: Convert QByteArray to quint 16

    Hello ChrisW i got a suggestion from another person with the below code, it worked Thank You so much you helped me a lot with my problem.

    Qt Code:
    1. quint16 r;
    2. for (int i = 0; i < data.size(); i+=2)
    3. {
    4. r = (((quint16) data.at(i) << 5) |(data.at(i+1)));
    5. char *ptrR = (char*) &r;
    6. newdat.append(ptrR[1]);
    7. newdat.append(ptrR[0]);
    8. }
    To copy to clipboard, switch view to plain text mode 

    I even tried your code but there is no toHex() in QList so i am getting a error there.

    what does you mean by
    You have to convert result to hex entry by entry , If you don't need the result retained in binary form (i.e. the result QList) then you can build the hex string as you go along
    Do i have to convert the QList to QByteArray again or what. ?? i wanted to learn how we can do this the way you have told also.

    Thank you so much again ChrisW

  20. #15
    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: Convert QByteArray to quint 16

    You kept saying you wanted to obtain a set of 16 bit integers. You can convert these to a hex string int by int: there is no toHex() method in QList, and there is no QByteArray involved.

    There are a lot of ways to do things vaguely like your request but you have to actually understand what you are trying to achieve and know some basic programming.

    Qt Code:
    1. QList<quint16> result; // A list of 16 bit unsigned integers
    2. for (int i = 0; i < ba.size(); i+=2)
    3. result.append( (static_cast<unsigned char>(ba.at(i) << 5)) | static_cast<unsigned char>(ba.at(i+1)) );
    4. // You now have a list of 16 bit ints like you requested.
    5.  
    6. // If you wanted only a hex string
    7. QString hex;
    8. for (int i = 0; i < ba.size(); i+=2) {
    9. quint16 r = static_cast<unsigned char>(ba.at(i) << 5)) | static_cast<unsigned char>(ba.at(i+1);
    10. result += QString(%1").arg(r, 4, 16, '0' ); // watch out for byte order if this needs to be portable
    11. }
    To copy to clipboard, switch view to plain text mode 

    Given that you already have the data in a buffer, and the result will be exactly the same size as the source data, then you could have done the job in-situ with a bit of thought:
    Qt Code:
    1. // char *buff
    2. // int numbytes
    3. for (int i = 0; i < numbytes; i+=2) {
    4. unsigned char a = static_cast<unsigned char>( buff[i] );
    5. unsigned char b = static_cast<unsigned char>( buff[i+1] );
    6. buff[i] = a >> 3;
    7. buff[i+1] = a << 5 | b;
    8. }
    To copy to clipboard, switch view to plain text mode 

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

    nagabathula (27th November 2010)

Similar Threads

  1. Shifting a QImage up by 1 pixel row
    By MSUdom5 in forum Qt Programming
    Replies: 2
    Last Post: 7th May 2010, 10:25
  2. LineEdit for Hexadecimal input
    By mastupristi in forum Qt Programming
    Replies: 2
    Last Post: 21st January 2010, 14:51
  3. how to process hexadecimal
    By mohanakrishnan in forum Qt Programming
    Replies: 2
    Last Post: 20th November 2009, 04:33
  4. Byte shifting in C
    By tntcoda in forum General Programming
    Replies: 3
    Last Post: 14th November 2008, 22:40
  5. Replies: 2
    Last Post: 4th August 2008, 08:14

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.