Results 1 to 3 of 3

Thread: Read unicode 8 bit and 16 bit data into QByteArray : Qt, C++, Windows XP

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

    Default Read unicode 8 bit and 16 bit data into QByteArray : Qt, C++, Windows XP

    hi,
    i'm developing a propriety protocol for my company (programming on the server side) and have a very strict deadline. It was decided that we use Qt. It is required to querry the Window Registry and write the value obtained into a prenegotiated socket. I have read the Registry Data into a QString. For certain data I must use 8-bit unicode characters and for some 16-bit unicode characters. I am using QByteArray to store all the data before finally writing to the socket using QTcpSocket::write(). Little Endianness must be followed.
    <1> How do i get the data from QString into QByteArray in unicode 8 bit format (specification says character type corresponds to quint8)?
    <2> How do i get the data from QString into QByteArray in unicode 16 bit format (specification says character type corresponds to quint16)?
    <3> How to maintain Little Endianness on point number 2?

    Thank you

  2. #2
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Read unicode 8 bit and 16 bit data into QByteArray : Qt, C++, Windows XP

    Unfortunately your specifications are incomplete: "unicode 8 bit format" and "unicode 16 bit format" are not clearly defined names for Unicode encodings; but, from your description, they seem to correspond to UTF-8 and UTF-16LE respectively. Please make sure that it is indeed what you need, and that you must not include any BOM (byte-order mark). Then you can set a QTextCodec up with the chosen encoding to convert between QStrings and QByteArrays.

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

    ustulation (24th June 2012)

  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: Read unicode 8 bit and 16 bit data into QByteArray : Qt, C++, Windows XP

    There does not seem to be an elegant way to turn off the UTF-16 byte-order-mark in QTextCodec.

    Qt Code:
    1. wchar_t testString[] = { 0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x20,
    2. 0x928, 0x92E, 0x938, 0x94D, 0x924, 0x947,
    3. 0x00}; // some ASCII some Devanagari
    4. QString fakeStringFromRegistry = QString::fromWCharArray(testString);
    5. qDebug() << "Original string" << fakeStringFromRegistry;
    6.  
    7. QTextCodec *codec = QTextCodec::codecForName("UTF-8");
    8. QByteArray encodedString = codec->fromUnicode(fakeStringFromRegistry);
    9. qDebug() << "UTF-8" << encodedString.toHex();
    10.  
    11. codec = QTextCodec::codecForName("UTF-16LE");
    12. encodedString = codec->fromUnicode(fakeStringFromRegistry);
    13. qDebug() << "UTF-16LE with byte-order-mark " << encodedString.toHex();
    14. qDebug() << "UTF-16LE without byte-order-mark" << encodedString.mid(2).toHex();
    To copy to clipboard, switch view to plain text mode 

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

    ustulation (24th June 2012)

Similar Threads

  1. Replies: 0
    Last Post: 7th June 2012, 21:58
  2. Replies: 6
    Last Post: 30th March 2012, 08:01
  3. Replies: 11
    Last Post: 21st February 2011, 17:54
  4. can't read from unicode file .
    By gbmtoday in forum Qt Programming
    Replies: 6
    Last Post: 21st February 2010, 14:36
  5. How to read a Number from the QByteArray
    By antonio.r.tome in forum Qt Programming
    Replies: 2
    Last Post: 24th February 2006, 15:24

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.