Note that there are some explanatory texts on larger screens.

plurals
  1. POHex to String Conversion C++/C/Qt?
    primarykey
    data
    text
    <p>I am interfacing with an external device which is sending data in hex format. It is of form</p> <pre><code>&gt; %abcdefg,+xxx.x,T,+yy.yy,T,+zz.zz,T,A*hhCRLF </code></pre> <ul> <li>CR LF is carriage return line feed</li> <li>hh->checksum</li> <li>%abcdefg -> header </li> </ul> <p>Each character in above packet is sent as a hex representation (the xx,yy,abcd etc are replaced with actual numbers). The problem is at my end I store it in a const char* and during the implicit conversion the checksum say 0x05 is converted to \0x05. Here \0 being null character terminates my string. This is perceived as incorrect frames when it is not. Though I can change the implementation to processing raw bytes (in hex form) but I was just wondering whether there is another way out, because it greatly simplifies processing of bytes. And this is what programmers are meant to do.</p> <p>Also in cutecom (on LINUX RHEL 4) I checked the data on serial port and there also we noticed <code>\0x05</code> instead of 5 for checksum. Note that for storing incoming data I am using</p> <pre><code>//store data from serial here unsigned char Buffer[SIZE]; //convert to a QString, here is where problem arises QString str((const char*)Buffer); of \0 </code></pre> <p>QString is "string" clone of Qt. Library is not an issue here I could use STL also, but C++ string library is also doing the same thing. Has somebody tried this type of experiment before? Do share your views.</p> <p>EDIT</p> <p>This is the sample code you can check for yourself also:</p> <pre><code>#include &lt;iostream&gt; #include &lt;string&gt; #include &lt;QString&gt; #include &lt;QApplication&gt; #include &lt;QByteArray&gt; using std::cout; using std::string; using std::endl; int main(int argc,char* argv[]) { QApplication app(argc,argv); int x = 0x05; const char mydata[] = { 0x00, 0x00, 0x03, 0x84, 0x78, 0x9c, 0x3b, 0x76, 0xec, 0x18, 0xc3, 0x31, 0x0a, 0xf1, 0xcc, 0x99}; QByteArray data = QByteArray::fromRawData(mydata, sizeof(mydata)); printf("Hello %s\n",data.data()); string str("Hello "); unsigned char ch[]={22,5,6,7,4}; QString s((const char*)ch); qDebug("Hello %s",qPrintable(s)); cout &lt;&lt; str &lt;&lt; x ; cout &lt;&lt; "\nHello I am \0x05"; cout &lt;&lt; "\nHello I am " &lt;&lt; "0x05"; return app.exec(); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload