QString 转 char 和 unsigned char

1
2
3
4
5
6
7
8
9
10
QString s("tttttest");

int len = strlen((char *)(s.toLatin1().data()));
char *cs = (char*)malloc(sizeof(char) * len);
strcpy(cs, (char *)(s.toLatin1().data()));
qDebug() << "char*" << cs;

unsigned char * us = (unsigned char *)cs;
qDebug() << "unsigned char:" << us;
free(cs);

unsigned char 转 QString

1
2
QString ss(QLatin1String((char *)us));
qDebug() << "QString:" << ss;
1
2
3
4
QString s("test");
qDebug() << QString(QLatin1String(
(char*)(unsigned char *)s.toLatin1().data()
));

注意:不支持中文这类双字节