Results 1 to 8 of 8

Thread: Using Multi-Byte in place of Unicode

  1. #1
    Join Date
    Mar 2010
    Posts
    11
    Thanks
    4
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Using Multi-Byte in place of Unicode

    I have some code that was written for windows. One of the parameters requires multi-byte instead of unicode. In Visual Studio 2008 I can change that in Project->Class Properties->Configuration Set. Is it possible to do this in Qt 4? The project I am working on right now requires Multi-Byte input vs Unicode, but I cannot seem to change/convert it to work right.

    Thanks

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

    Default Re: Using Multi-Byte in place of Unicode

    Qt uses Unicode internally, I doubt you can change that. But converting to wchar* (or std::wstring) should be possible (QString::toStdWString()).
    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 2010
    Posts
    11
    Thanks
    4
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Using Multi-Byte in place of Unicode

    Thanks for the speedy reply sir!
    Buuuuut it didn't work as easy as I had hoped .

    Here is the code that is breaking.

    Qt Code:
    1. char adjust_device[16];
    2. sprintf(adjust_device, "\\\\.\\%s", device);
    3. HComm = CreateFile(adjust_device, GENERIC_READ | GENERIC_WRITE, 0,
    4. NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
    5. if (HComm == INVALID_HANDLE_VALUE) {
    6. return -1;
    7. }
    To copy to clipboard, switch view to plain text mode 

    I tried to do this:
    Qt Code:
    1. sprintf(QString::toStdWString(adjust_device), "\\\\.\\%s", device);
    2. HComm = CreateFile(adjust_device, GENERIC_READ | GENERIC_WRITE, 0,
    3. NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
    To copy to clipboard, switch view to plain text mode 

    and I get the following error: no matching function call toQString::toStdWstring(char[16])

    Any ideas?

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

    Default Re: Using Multi-Byte in place of Unicode

    It's not a static method, that's one thing. The other is that it will convert the string to std::wstring, just as the name of the method says and not to a char*. The device name probably contains ascii characters only anyway, so you can probably do this:

    Qt Code:
    1. QString device = "<something goes here">;
    2. QString adjustDeviceStr = "\\\\.\\"+device;
    3. QByteArray ba = adjustDeviceStr.toAscii();
    4. HComm = CreateFile(ba.constData(), ...);
    To copy to clipboard, switch view to plain text mode 
    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.


  5. #5
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Using Multi-Byte in place of Unicode

    There's two versions of CreateFile:
    CreateFileA takes a ASCII parameters (char *)
    CreateFileW takes Unicode parameters (wchar *)

    Typically, if you have Unicode defined, CreateFileW is aliases to CreateFile, else CreateFileA is aliased to CreateFile.

    You can still use either by using the proper names:

    Qt Code:
    1. QString foo = "\\\\.\\COM1";
    2. CreateFileW(foo.utf16(), ...)
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. char foo[] = "\\\\.\\COM1";
    2. CreateFileA(foo, ...)
    To copy to clipboard, switch view to plain text mode 

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

    TheDuncan (12th March 2010)

  7. #6
    Join Date
    Mar 2010
    Posts
    11
    Thanks
    4
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Using Multi-Byte in place of Unicode

    Thanks a bunch. I used CreateFileA and it seems to be working.

    I'm new to Qt so I have a noob question to ask. How can I display a long in a Line Edit? I tried this

    Qt Code:
    1. QString number(lmsDist[20], 10);
    2. ui->txtAhead->setText(number);
    To copy to clipboard, switch view to plain text mode 

    but it just inserts blank spaces. I used debugging mode to check what lmsDist[540] was and it was an actual number (6530, 87, etc). What am I doing wrong?

    Thanks again with your help.

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

    Default Re: Using Multi-Byte in place of Unicode

    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.


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

    TheDuncan (12th March 2010)

  10. #8
    Join Date
    Mar 2010
    Posts
    11
    Thanks
    4
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Using Multi-Byte in place of Unicode

    Works like a champ. Thanks a million!

Similar Threads

  1. Converting from char to byte
    By Ferric in forum Newbie
    Replies: 2
    Last Post: 8th January 2010, 00:27
  2. Byte shifting in C
    By tntcoda in forum General Programming
    Replies: 3
    Last Post: 14th November 2008, 22:40
  3. Two Byte character Support
    By rajveer in forum General Programming
    Replies: 10
    Last Post: 25th October 2008, 00:29
  4. convert BYTE* to QString
    By tpf80 in forum Qt Programming
    Replies: 6
    Last Post: 8th April 2007, 10:29
  5. Can QByteArray takes place of BYTE?
    By hiuao in forum Qt Programming
    Replies: 2
    Last Post: 5th April 2007, 11:49

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.