[英]Convert QString to QByteArray with forced locale
I need to convert QString to QByteArray using cp1251 locale without toLocal8bit() function. 我需要使用cp1251语言环境将QString转换为QByteArray而不使用toLocal8bit()函数。
Ie on my current PC default locale is cp1251 -> QString::toLocal8bit() is working great, but once I switch locale to something different -> I cannot use toLocal8Bit(). 即我当前的PC默认语言环境是cp1251 - > QString :: toLocal8bit()工作得很好,但是一旦我将语言环境切换到不同的东西 - >我不能使用toLocal8Bit()。 I've read documentation about QTextCodec and stuff, but it seems very obscure to me. 我已经阅读了有关QTextCodec和东西的文档,但对我来说这似乎很模糊。
So, the question is - how do I convert from QString to QByteArray using specified locale? 所以,问题是 - 如何使用指定的语言环境从QString转换为QByteArray?
Any help and explanation will be very welcome. 任何帮助和解释都将非常受欢迎。
As you found out yourself, you'll need QTextCodec
for this. 当你发现自己时,你需要QTextCodec
。 What you want to do should be as simple as this: 你想做的应该是这么简单:
QString src = "My test string";
QTextCodec *codec = QTextCodec::codecForName("Windows-1251");
QByteArray encodedString = codec->fromUnicode(src);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.