Qt char* to QString

白昼怎懂夜的黑 提交于 2019-12-12 03:38:57

问题


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;


来源:https://stackoverflow.com/questions/13153281/qt-char-to-qstring

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!