Conversion of Symbian descriptor tbuf and const char *

Source: Internet
Author: User

 

Conversion between Buf and const char *

Http://wiki.forum.nokia.com/index.php/How_to_Convert_TBuf_to_Char_and_Vice_Versa

How to convert tbuf to Char and vice versa
From Forum Nokia Wiki

Void stringtodescriptor (const char * astring, TDES & adescriptor)
{
Tptrc8 PTR (reinterpret_cast <const tuint8 *> (astring ));
Adescriptor. Copy (PTR );
}

Usage:

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 ));
_ Partition (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: info-contact@alibabacloud.com 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.