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.
----------------------------------------------------
Turn: http://blog.csdn.net/9527/archive/2006/08/29/1137637.aspx
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 ";
Tptrc8;
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.
-----------------------------------------------------------------
Http://wiki.forum.nokia.com/index.php/How_to_Convert_TBuf_to_Char_and_Vice_Versa
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);}
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)); _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;}
--------------------------------------------------