Conversion bitween 8 and 16 bits descriptors
| Mon, 2003-12-15 22:38 | |
|
|
There is two kind of conversion between 8 and 16 bits constructor. The first type is just a cast where the data itself are not physically changed : Code: // Get a iBuf8 from a iBuf16 (data are not modified) TPtrC8 ptr8(reinterpret_cast<const TUint8*>(iBuf16.Ptr()),(iBuf16.Size()*2)); iBuf8=ptr8; // Get a iBuf16 from a iBuf8 (data are not modified) TPtrC16 ptr16(reinterpret_cast<const TUint16*>(iBuf8.Ptr()),(iBuf8.Size()/2)); iBuf16=ptr16; The second one takes each character and convert it to the other format. The 16-bit to 8-bit conversion may not always succeed in this case: Code: // Get a iBuf8 from a iBuf16 (data are modified) CnvUtfConverter::ConvertFromUnicodeToUtf8(iBuf8,iBuf16); // Get a iBuf16 from a iBuf8 (data are modified) CnvUtfConverter::ConvertToUnicodeFromUtf8(iBuf16,iBuf8); This second method requires to include the utf.h header and to link against charconv.lib. Many other methods exist, but these four ones are the one I am using. Eric Eric Bustarret |






Forum posts: 22
iBuf16.Size()*2
should be
iBuf16.Size()
or
iBuf16.Length()*2
Forum posts: 52
{
HBufC* myBuf = HBufC::NewLC(aDes8.Length());
myBuf->Des().Copy(aDes8);
return myBuf;
}
Forum posts: 78
TDes* k; // a pointer to TDes
HBufC16* key = k->AllocL();//I get a pointer to HBufC16
TPtr16 p = key->Des(); //here I get a TPtr16
keyword =(TPtr16*)&p // I need to store the pointer to the TPtr16 object in “keyword”.Is it the write way to store a pointer to do .
TPtrC8 ptr8(reinterpret_cast<const TUint8*>(iBuf16.Ptr()),(iBuf16.Size()*2));
iBuf8=ptr8;
// Get a iBuf16 from a iBuf8 (data are not modified)
TPtrC16 ptr16(reinterpret_cast<const TUint16*>(iBuf8.Ptr()),(iBuf8.Size()/2));
iBuf16=ptr16;
The second one takes each character and convert it to the other format. The 16-bit to 8-bit conversion may not always succeed in this case:
CnvUtfConverter::ConvertFromUnicodeToUtf8(iBuf8,iBuf16);
// Get a iBuf16 from a iBuf8 (data are modified)
CnvUtfConverter::ConvertToUnicodeFromUtf8(iBuf16,iBuf8);
This second method requires to include the utf.h header and to link against charconv.lib.
Many other methods exist, but these four ones are the one I am using.
Eric
Forum posts: 17
Undefined Symbol: TParse::TParse(void)
I've added charconv.lib and added #include <utf.h>
TBuf<60> a;
TBuf8<60> b;
b.Copy(a);
What could be wrong?
Shereen.
Forum posts: 25
dotcdotc
Forum posts: 1
iBuf16.Size()*2
should be
iBuf16.Size()
or
iBuf16.Length()*2
Using *2 might not be sufficient, because utf8 characters can consist of three and more charactetrs. Think of the euro sign (\x{20ac}) which occupies three bytes.
Forum posts: 68
TDes* k; // a pointer to TDes
HBufC16* key = k->AllocL();//I get a pointer to HBufC16
TPtr16 p = key->Des(); //here I get a TPtr16
keyword =(TPtr16*)&p // I need to store the pointer to the TPtr16 object in “keyword”.Is it the write way to store a pointer to do .
As far as I know TPtr is already a pointer to a modifiable descriptor. Why do you have an extra level of indirection? Also "TDes* k" declares a pointer to a descriptor... but where does it point to? Does this code work?
If we fall down it's so we can learn to pick ourselves up.
Forum posts: 68
I believe a topic on Unicode and the different encodings would be most appropriate.
Here's a link to get started:
http://www.joelonsoftware.com/articles/Unicode.html
Any volunteers?
Thanks,
Nikolas.
If we fall down it's so we can learn to pick ourselves up.
NOTE: The post by Eric is incorrect and solution by chenziteng works.
Forum posts: 68
First Symbian's internal string format is UCS-2/UTF-16 (they are the same thing).
Now I don't know if you're familiar with the Unicode/ISO-10646 standard. So I suggest you look up the definitions I use... Wikipedia has an excellent description and of course there's unicode.org.
All code points can be assigned a 16 bit number. However not all code points map to valid characters. Some code points are reserved. You have 2 groups of code points known as high surrogates and low surrogates. These code points should only be used in pairs of 1 high surrogate and 1 low surrogate. These surrogate pairs are used to encode the least used characters. The result is that effectively unicode characters that fall in this category need 2 16-bits code points to be encoded. Thus some characters need to be represented with 32 bits. These characters are however relatively rare.
Also about UTF-8. As you probably know UTF-8 is a "clever" variable length encoding. However it has been limited to 4 bytes for UCS-2/UTF-16 compatibilty.
I'm currently doing a little "research" on the Unicode/ISO-106406 standard and I'll putting together some presentation at some point which of course I'll make freely available to anyone.
To be continued...
Thanks,
Nikolas.
If we fall down it's so we can learn to pick ourselves up.
Forum posts: 8
TBuf8<4> b(a);
TBuf<2> c;
CnvUtfConverter::ConvertToUnicodeFromUtf8(c,b);
c is supposed to be "流年",it's value are 27969,24180
but is not,the value it contain are 65533,65533
why?
Forum posts: 68
The first byte indicates the number of bytes that will be used to encode the character, the rest actually encode the character.
I suspect you need more that then 2 bytes per character if you are to use UTF-8 to encode those characters.
Have a quick look on Wikipedia to see if that's the problem....
If we fall down it's so we can learn to pick ourselves up.
Forum posts: 32
I want to write /read the TBuf to the Filestream.
My converting code is not working. Please give me any advice!
HBufC16* TUEmailSettings::Convert8To16BitLC(const TDesC8& aDes8)
{
//HBufC* myBuf = HBufC::NewLC(aDes8.Length());
//TPtr ptr = myBuf->Des();
//ptr.Copy(aDes8);
//return ptr;
HBufC16* myBuf16 = HBufC16::NewLC(aDes8.Length());
TPtrC16 ptr16(reinterpret_cast<const TUint16*>(aDes8.Ptr()),(aDes8.Size()/2));
myBuf16=ptr16;
return myBuf16;
// Get a iBuf16 from a iBuf8 (data are not modified)
//TPtrC16 ptr16(reinterpret_cast<const TUint16*>(iBuf8.Ptr()),(iBuf8.Size()/2));
//iBuf16=ptr16;
}
HBufC8* TUEmailSettings::Convert16To8BitLC(const TDesC& aDes)
{
//HBufC8* myBuf8 = HBufC8::NewLC(aDes.Length());
//TPtr8 ptr8 = myBuf8->Des();
//ptr8.Copy( aDes );
//return ptr8;
HBufC8* myBuf8 = HBufC8::NewLC(aDes.Length());
TPtrC8 ptr8(reinterpret_cast<const TUint8*>(aDes.Ptr()),(aDes.Size()*2));
myBuf8=ptr8;
return myBuf8;
//TPtrC8 ptr8(reinterpret_cast<const TUint8*>(iBuf16.Ptr()),(iBuf16.Size()*2));
//iBuf8=ptr8;
}