Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Developer
shahumang8
Posts: 581
Registered: ‎08-09-2010
My Device: Not Specified
Accepted Solution

QString not convert into UTF8

HI All,

 

   I attached one sample app which i parse soap api and got reponse from webservice and i convert reponse into UTF8 but QString not convert into UTF8 so can you check my app??

 

  I convert like below but still not get 

 

  

QString finalVal = responseValue.toString();
finalVal = finalVal.left(finalVal.length() - 1);
finalVal = finalVal.mid(1);
QString text = QString::fromUtf8(finalVal.toStdString().c_str());
termsWebView->setHtml(text);

 Note :- If i put static QString then they convert into UTF8 but i put parser QString then they not convert into UTF8.

Please use plain text.
Developer
Zmey
Posts: 1,510
Registered: ‎12-18-2012
My Device: PlayBook, Z10, DAC

Re: QString not convert into UTF8

[ Edited ]

Hi,

This line:
QString finalVal = responseValue.toString();
implicitly converts the string from LATIN1 encoding.

Use:
QString finalVal = QString::fromUtf8(responseValue.data(), responseValue.size());
Then perform the manipulations on finalVal.

 


Andrey Fidrya, @zmeyc on twitter
Please use plain text.
Developer
shahumang8
Posts: 581
Registered: ‎08-09-2010
My Device: Not Specified

Re: QString not convert into UTF8

Hi zmey,

I also try sane but not get success. I request to you please check my sample app because I have some doubt about which string get from webservices it's not proper.
Please use plain text.
BlackBerry Development Advisor (Retired)
robbieDubya
Posts: 418
Registered: ‎07-18-2012
My Device: Q10
My Carrier: Bell Canada

Re: QString not convert into UTF8

Seems to be an XML encoding issue - not UTF8.

 

I see correct characters in the test app - but your HTML is being mangled. eg: < has become something like \u003e.

 

You need to unescape those characters. There should be a Qt built in way to do this - but i didn't find it - so - try this...

 

		QString finalVal(responseValue.toString());
		QString text(finalVal.mid(2, finalVal.length() - 4));

		QString result;
		for (int i = 0; i < text.length(); ++i) {
			QChar next = text[i];
			if (next == '\\' && i < text.length() - 1) {
				next = text[++i];
				if (next != 'u') {
					result += next;
				} else {
					if (i<text.length()-4) {
						QString num;
						for (int j=0; j<4; ++j) {
							num += text[++i];
						}
						bool b = false;
						// boldly ignoring false.
						QChar x(num.toInt(&b,16));
						result += x;
					} else {
						result += "\\u";
						continue;
					}
				}
			} else {
				result += next;
			}
		}
		termsWebView->setHtml(result);

 

--
Rob is no longer associated with BlackBerry.
Please use plain text.
Developer
shahumang8
Posts: 581
Registered: ‎08-09-2010
My Device: Not Specified

Re: QString not convert into UTF8

Hi RobbieDubya,

 

   It's not conver \r\n character.

 

   Before reponse :- 

 

    

\u003cp style=\"text-align: center; margin: 0px\"\u003e\u003cspan style=\"font-size: 18pt; font-weight: bold; text-decoration: underline\"\u003eAVISO LEGAL\u003c/span\u003e\u003c/p\u003e\u003cbr /\u003e\r\n\u003cp class=\"MsoNormal\" style=\"text-align: justify; margin: 0cm 0cm 0pt\"\u003e\u003cb\u003e\u003cspan lang=\"ES\" style=\"color: #002060; mso-ansi-language: es\"\u003e\u003cfont size=\"3\"\u003e\u003cfont face=\"Calibri\"\u003eLa Boliviana Ciacruz de Seguros y Reaseguros S.A., deja claramente establ\u003c/font\u003e\u003c/font\u003e\u003c/span\u003e\u003c/b\u003e\u003cb\u003e\u003cspan lang=\"ES\" style=\"color: #002060; mso-ansi-language: es\"\u003e\u003cfont size=\"3\"\u003e\u003cfont face=\"Calibri\"\u003eecido que el alcance de este servicio de aviso de accidente, creado como aplicación para smartphones para nuestros asegurados, es simplemente un aviso no formal que debe necesariamente ser formalizado a través del llenado  del formulario impreso de Denuncia de Accidente o Robo y la documentación requerida para la atención del mismo. Las condiciones previamente expuestas son de  pleno conocimiento del asegurado, quien acepta de manera voluntaria e irrevocable las mismas, presionando a tal efecto  el  botón “Acepto�.\u003c/font\u003e\u003c/font\u003e\u003c/span\u003e\u003c/b\u003e\u003cb\u003e\u003cspan lang=\"ES\" style=\"color: #002060; mso-ansi-language: es\"\u003e\u003cfont size=\"3\"\u003e\u003cfont face=\"Calibri\"\u003e\r\n\u003co:p\u003e\u003c/o:p\u003e\u003c/font\u003e\u003c/font\u003e\u003c/span\u003e\u003c/b\u003e\u003c/p\u003e" 

 

   After i get below email

 

    

<p style="text-align: center; margin: 0px"><span style="font-size: 18pt; font-weight: bold; text-decoration: underline">AVISO LEGAL</span></p><br />rn<p class="MsoNormal" style="text-align: justify; margin: 0cm 0cm 0pt"><b><span lang="ES" style="color: #002060; mso-ansi-language: es"><font size="3"><font face="Calibri">La Boliviana Ciacruz de Seguros y Reaseguros S.A., deja claramente establ</font></font></span></b><b><span lang="ES" style="color: #002060; mso-ansi-language: es"><font size="3"><font face="Calibri">ecido que el alcance de este servicio de aviso de accidente, creado como aplicación para smartphones para nuestros asegurados, es simplemente un aviso no formal que debe necesariamente ser formalizado a través del llenado  del formulario impreso de Denuncia de Accidente o Robo y la documentación requerida para la atención del mismo. Las condiciones previamente expuestas son de  pleno conocimiento del asegurado, quien acepta de manera voluntaria e irrevocable las mismas, presionando a tal efecto  el  botón “Acepto�.</font></font></span></b><b><span lang="ES" style="color: #002060; mso-ansi-language: es"><font size="3"><font face="Calibri">rn<o:p></o:p></font></font></span></b></p>" 

  

   May i know how to solved??

Please use plain text.
BlackBerry Development Advisor (Retired)
robbieDubya
Posts: 418
Registered: ‎07-18-2012
My Device: Q10
My Carrier: Bell Canada

Re: QString not convert into UTF8

You'd have to make sure that they're de-escaped as well.

 

eg:

if (next=='r') {

  result += "\r";

} else if (next=='n') {

  . . . 

} else if (next!='u') {

 . . . (as is)

} else {

 . . . (as is)

}

--
Rob is no longer associated with BlackBerry.
Please use plain text.
BlackBerry Development Advisor (Retired)
robbieDubya
Posts: 418
Registered: ‎07-18-2012
My Device: Q10
My Carrier: Bell Canada

Re: QString not convert into UTF8

ps: \t is common too - as a tab char...

--
Rob is no longer associated with BlackBerry.
Please use plain text.