Conversion between TDes8 and TDes16 is a common headache in Symbian development. At least for developpers used to work with 8 bits character coding.
Symbian’s CnvUtfConverter class is a powerful framework to handle character conversions to and from UTF8 (i.e TDes8). In most cases however, you only handle plain ASCII characters, using TDes8 to read/write to a file or a socket and TDes/TDes16 for the internal handling of the data. The following usage of CnvUtfConverter is suitable in this case (KErrNone is return in case of success, a positive value represents the number of aFrom characters not converted):
#include <utf.h> // charconv.lib
TInt NNewLCUtils::ConvertTDes8ToTDes16(const TDesC8& aFrom, TDes16& aTo)
{
return(CnvUtfConverter::ConvertToUnicodeFromUtf8(aTo,aFrom));
}
TInt NNewLCUtils::ConvertTDes16ToTDes8(const TDesC16& aFrom, TDes8& aTo)
{
return(CnvUtfConverter::ConvertFromUnicodeToUtf8(aTo,aFrom));
}
If you are dealing with UTF-7 (RFC-2152), you can also take a look at the ConvertFromUnicodeToUtf7 / ConvertFromUnicodeToUtf7 methods (note that there is no direct convesion between UTF-7 and UTF-8).
|