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
Highlighted
Developer
Posts: 194
Registered: ‎11-24-2012
My Device: Dev Alpha B, Bold 9700
My Carrier: NA
Accepted Solution

help understanding localization and fromUtf8 method

I create share data like this:

QString data = QString::fromUtf8(tr("some localized string"));
query->setData(data.toUtf8());

 Why do the examples show fromUtf8 / toUtf8 when many of the languages I support need Utf16? Is it okay to use Utf16 or will something happen I'm not aware of?

 

Developer
Posts: 1,524
Registered: ‎12-18-2012
My Device: Z30, Z10 LE, DevAlpha C, PlayBook

Re: help understanding localization and fromUtf8 method

[ Edited ]

UTF8 was created for backwards compatibility with ASCII and lower 7 bits match ASCII encoding. It can be used with std C functions like strlen() without any conversion.

In UTF8 one character can take up to 6 bytes and it's capable of storing all characters UTF16 can store.

Source code files are usually created in UTF8, but QString uses UTF16 internally, that's why fromUtf8 conversion is needed.

 

p.s.

For network communication UTF8 is usually better because it doesn't depend on byte order.

Byte order for UTF16 can depend on HW architecture. Also UTF16 text sometimes has BOM (byte order marker) prepended to assist in determining the byte order of the characters. UTF8 doesn't require it.

 

One of UTF16 advantages is memory usage: UTF8 can take 3 bytes per character for non-ASCII text while UTF16 uses 2 most of the time.

 


Andrey Fidrya, @zmeyc on twitter
Developer
Posts: 194
Registered: ‎11-24-2012
My Device: Dev Alpha B, Bold 9700
My Carrier: NA

Re: help understanding localization and fromUtf8 method

[ Edited ]

Can I skip the conversion with BB 10 when using it in my sharing method?

Developer
Posts: 1,524
Registered: ‎12-18-2012
My Device: Z30, Z10 LE, DevAlpha C, PlayBook

Re: help understanding localization and fromUtf8 method

[ Edited ]

If function expects const char * or ByteArray and encoding isn't specified then most likely it expects UTF8-encoded string. It should be zero-terminated in case of const char *.

 

I've used UTF8 strings in InvokeActionItem for BBM sharing and non-ascii characters were displayed correctly.

 

QString data = QString::fromUtf8("text");
query->setData(data.toUtf8()); // setData expects ByteArray

Andrey Fidrya, @zmeyc on twitter
Developer
Posts: 194
Registered: ‎11-24-2012
My Device: Dev Alpha B, Bold 9700
My Carrier: NA

Re: help understanding localization and fromUtf8 method

My biggest concern is East Asian Languages, which require UTF 16.  Have you tried any of those characters with a InvokeAction?

Developer
Posts: 1,524
Registered: ‎12-18-2012
My Device: Z30, Z10 LE, DevAlpha C, PlayBook

Re: help understanding localization and fromUtf8 method

[ Edited ]

I've enabled Chinese input in language settings. Text I typed in application was passed unchanged into BBM so it seems to work. UTF8 is capable of representing the entire characters range.


Andrey Fidrya, @zmeyc on twitter
Developer
Posts: 194
Registered: ‎11-24-2012
My Device: Dev Alpha B, Bold 9700
My Carrier: NA

Re: help understanding localization and fromUtf8 method

Thank you once again Zmey!