Actually the packet data I am trying to send is like the ASCII form of "02000204690001000001"
What do you mean "the ASCII form of" in relation to a perfectly good ASCII string?
I think you more urgently need to understand what hexadecimal is: it is a human-readable representation of a number in base 16. A hexadecimal string is an ASCII string representing a collection of bytes that may not have values valid in an ASCII string, i.e. any byte value over 127, or inconvenient in a string like zero bytes.
Assuming that string is in a QString variable s:
iodev.write( buf );
QByteArray buf = s.toLatin1();
iodev.write( buf );
To copy to clipboard, switch view to plain text mode
Will send that string through the device. That is a series of 20 bytes with values (decimal): 48 50 48 48 48 50 etc.
If you mean the string is the hexadecimal representation of a series of 10 bytes then:
iodev.write( buf );
QByteArray buf = QByteArray::fromHex( s.toLatin1() );
iodev.write( buf );
To copy to clipboard, switch view to plain text mode
Will write ten bytes with the values (decimal): 2 0 2 4 105 0 1 0 0 1
There is no need to convert binary data to hexadecimal strings in Qt unless you are doing it to display to a human.
Bookmarks