Qt char* to QString

2019-09-13 09:28发布

问题:

I am trying to convert char* to QString. It should be a trivial task but the problem is, I am having the following input:

item    char [512] "N"  char [512]
    [0] 78 'N'  char
    [1] 0 '\0'  char
    [2] 73 'I'  char
    [3] 0 '\0'  char
    [4] 70 'F'  char
    [5] 0 '\0'  char
    [6] 84 'T'  char
    [7] 0 '\0'  char
    [8] 89 'Y'  char
    [9] 0 '\0'  char
    [10]    0 '\0'  char

Notice the null character after each character '\0'. Simply trying to convert it just yields the string "N" where as it should result into string "NIFTY".

I am not sure if it is unicode or Ansi string (in fact I don't know much about it). Can anyone please sort out what is going wrong here or what am I missing?

回答1:

This worked for me:

    char * chr = "N\0I\0F\0T\0Y\0\0";
    QString str = QString::fromUtf16((ushort*)(chr));
    qDebug() << str;