Hey guys,
I want to convert a QString to utf8 html compliant so it can be viewed in a QWebPage.
Qt Code:
To copy to clipboard, switch view to plain text mode
I tried the above with no succes.
Hey guys,
I want to convert a QString to utf8 html compliant so it can be viewed in a QWebPage.
Qt Code:
To copy to clipboard, switch view to plain text mode
I tried the above with no succes.
I am not sure what QWebPage got to do with UTF.
Though to convert QString to UTF, use this:
Consider this:
That doesn't print the UTF8 byte sequence.Qt Code:
for (int i = 0; i < string.size(); i++) { qDebug("%c", string.at(i)); }To copy to clipboard, switch view to plain text mode
You can use:
I want to print the full byte sequence.UTF-8 encodes each character (code point) in 1 to 4 octets (8-bit bytes), with the single octet encoding used only for the 128 US-ASCII characters
see http://www.tony-franks.co.uk/UTF-8.htm
Qt Code:
char *data = ba.data(); while (*data) { cout << "[" << *data << "]" << endl; ++data; }To copy to clipboard, switch view to plain text mode
Qt Code:
char *data = ba.data(); while (*data) { qkDebug("[%c]", *data); ++data; }To copy to clipboard, switch view to plain text mode
Output:
This is not the conversion I want.
I expect "[&][#][1][6][9][;]";.
Last edited by bunjee; 27th August 2009 at 10:08.
You have used %c, and it will print the character represented by byte. If you want a hexadecimal byte representation of your string. Why dont you use old school printf.
Qt Code:
char *data = ba.data(); while (*data) { printf("%x", *data); ++data; }To copy to clipboard, switch view to plain text mode
Thans for the replies,
EDIT: What I want is an UTF8 html encoded string.
Last edited by bunjee; 27th August 2009 at 10:19.
If you can theoretically prove it.
I will give you code.
When you want to print a byte sequence, you have to decide the representation. A possible representation is hex. That I already told you how to do that. But your expected result is :
I dont know what is it. Can you please tell me whats in side []. a character/hex value/int /or something else.
bunjee (27th August 2009)
You want the sgml entity, not a "utf-8 sequence" - there is no such thing.
Qt will not encode sgml entities by itself. You need to do that manually:
Qt Code:To copy to clipboard, switch view to plain text mode
This is a rough implementation which will not work for multi-octet characters - you need to adjust the if block for that.
bunjee (27th August 2009)
Nothing to add,
I missed the point on this one.
Thanks guys.
Here is a first shot of a "toHtmlUtf8" method in my inherited QString class:
Qt Code:
{ QString result; for (int i = 0; i < size(); i++) { char ascii = c.toAscii(); if (ascii == ' ') { result += " "; } else if (ascii == '\n') { result += "<br>"; } else if (ascii < 48 || ascii > 122) { if (bytes.size() == 1) { } else { // FIXME // Insert multi byte code here } else result += "[?]"; } else result += c; } return result; }To copy to clipboard, switch view to plain text mode
Little help on the FIXME wouldn't hurt.
Bookmarks