Symbian basic data type conversion Collection (1)

Source: Internet
Author: User

The conversion of data types is big every time you touch it. You can only search for the data on the Internet. Specifically, you can make a collection of the basic data types you have searched.





1. String Conversion to numbers
Tbuf16 <20> Buf (_ L ("123 "));
Tlex Lex (BUF );
Tint inum;
Lex. Val (inum );
2. convert numbers into strings
Tbuf16 <20> Buf;
Tint inum = 20;
Buf. Format (_ L ("% d"), inum );
3. convert a Symbian string to a char string
Char * P = NULL;
Tbuf8 <20> Buf (_ L ("AAAAA "));
P = (char *) BUF. PTR ();

4. UTF-8 to Unicode
Cnvutfconverter: converttounicodefromutf8 (ibuf16, ibuf8 );
5. Unicode conversion to UTF-8
Cnvutfconverter: convertfromunicodetoutf8 (ibuf8, ibuf16 );

6. convert a char string to a Symbian string
Char * Cc = "aaaa ";
A. Set (const tuint8 *) CC, strlen (CC ));

Add one more:
Tdesc8 & Buf;
Tuint8 * pdata;
Pdata = Buf. PTR ();
Then, this pdata can be used as an unsigned char *, which is important for network communication.
If pdata is damaged, you can
Tbuf8 <1024> tmp_buf;
Tmp_buf.copy (BUF );
Pdata = tmp_buf.ptr ();
In this way, you can protect the data of the Buf, especially if the data received by the socket is allocated by the receiving function.



How to convert tbuf to Char and vice versafrom Forum Nokia Wiki

void stringToDescriptor(const char* aString, TDes& aDescriptor){    TPtrC8 ptr(reinterpret_cast<const TUint8*>(aString));    aDescriptor.Copy(ptr);}


const char* str = "Hello, world!";    TBuf<32> buffer;  // Make it large enough for str    stringToDescriptor(str, buffer);

Problem with the code above is that defining a tbuf not large enough will raise a user 23 panic.

Some ways to avoid this include:

Using either _ assert_debug or _ assert_always, depending on your needs. Note that you can use alternatives to user: panic (), as long as you avoid executing sensitive code.

void stringToDescriptor(const char* aString, TDes& aDescriptor){    TPtrC8 ptr(reinterpret_cast<const TUint8*>(aString));    _LIT(KMyPanicDescriptor, "My panic text");    __ASSERT_ALWAYS(User::StringLength(reinterpret_cast<const TUint8*>(aString)) <= aDescriptor.MaxLength(), User::Panic(KMyPanicDescriptor, 0));    aDescriptor.Copy(ptr);}

The other way is relying on a dynamic buffer, using hbufc for instance:

HBufC* stringToDescriptorL(const char* aString){    TPtrC8 ptr(reinterpret_cast<const TUint8*>(aString));    HBufC* buffer = HBufC::NewL(ptr.Length());    buffer->Des().Copy(ptr);     return buffer;}

Note that the caller is responsible of freeing the hbufc returned. Also note the trailing "L" in the function's name.

Depending on your code, you may prefere one of these over the others. Also, you may need to add extra checks (for instance, checking whether the char pointer is null or not ).

Converting descriptors to C-strings may be done this way:

const char* descriptorToStringL(const TDesC& aDescriptor){    TInt length = aDescriptor.Length();     HBufC8* buffer = HBufC8::NewLC(length);    buffer->Des().Copy(aDescriptor);     char* str = new(ELeave) char[length + 1];    Mem::Copy(str, buffer->Ptr(), length);    str[length] = '/0';     CleanupStack::PopAndDestroy(buffer);     return str;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.