TDesC是所有字元類的祖先
標準C語言
Symbian OS
讓一個字串進入2進位代碼
Static char hellorom[]=”hello”
_LIT(khellorom,”hello”)
在棧中獲得字串的指標
Const char* helloptr=hellorom
TPtrC helloptr=khellorom
獲得在棧中字串的指標
Char hellostack[sizeof(hellorom)];
Strcpy(hellostack,hellorom);
TBufC<5> hellostack=khellorom;
獲得在堆中字串的指標
Char* helloheap=
(char *)malloc(sizeof(hellorom));
strcpy(helloheap,hellorom);
HBufC* helloheap=
Khellorom.AllocLC();
a)TPtrC相當於不變的字串常量.
b)TPtr相當與String類型。Tbuf相當於char[]。前者與後者的唯一區別是,後者需要指定分配的棧空間大小。
C)HBufC* 與char*類似。分配的是堆上的空間。
HBufC* textResource;
//兩種字串附值方法
textResource = StringLoader::LoadLC( R_HEWP_TIME_FORMAT_ERROR );
textResource =iEikonEnv->AllocReadResourceL(R_EXAMPLE_TEXT_HELLO);
TBuf<32> timeAsText;
timeAsText = *textResource;
/* 資料類型轉換*/
TBuf 轉換為 TPtrC16
TBuf<32> tText(_L("2004/11/05 05:44:00"));
TPtrC16 tPtrSecond=tText.Mid(17,2);
TPtrC16 轉換為 TBufC16
TPtrC16 tPtrSecond=tText.Mid(17,2);
TBufC16<10> bufcs(tPtrSecond);
TBufC16 轉換為 TPtr16
TBufC16<10> bufcs(tPtrSecond);
TPtr16 f=bufcs.Des();
TPtr16 轉換為 TBuf
TBuf<10> bufSecond;
bufSecond.Copy(f);
TBuf 轉換為 TPtr16
TBuf<10> bufSecond(_L("abc"));
TPtr16 f;
f.Copy(bufSecond);
TBuf 轉換為 TInt
TInt aSecond;
TLex iLexS(bufSecond);
iLexS.Val(aSecond);
TInt 轉換為 TBuf
TBuf<32> tbuf;
TInt i=200;
tbuf.Num(i);
1.串轉換成數字
TBuf16<20> buf(_L( "123" ) );
TLex lex( buf );
TInt iNum;
lex.Val( iNum );
2.數字轉換成串
TBuf16<20> buf;
TInt iNum = 20;
buf.Format( _L( "%d" ) , iNum );
3.將symbian串轉換成char串
char* p = NULL;
TBuf8<20> buf( _L( "aaaaa" ) );
p = (char *)buf.Ptr();
4.UTF-8轉換成UNICODE
CnvUtfConverter::ConvertToUnicodeFromUtf8( iBuf16 , iBuf8 );
5.UNICODE轉換成UTF-8
CnvUtfConverter::ConvertFromUnicodeToUtf8( iBuf8 , iBuf16 );
6.將char串轉換成symbian串
char* cc = "aaaa";
TPtrC8 a;
a.Set( (const TUint8*)cc , strlen(cc) );
7、將TPtrc8與TPtrc16之間轉化
// 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.
/*memset memcpy strcpy */
memset主要應用是初始化某個記憶體空間。用來對一段記憶體空間全部設定為某個字元。
memcpy是用於COPY源空間的資料到目的空間中,用來做記憶體拷貝可以拿它拷貝任何資料類型的對象。
strcpy只能拷貝字串了,它遇到'"0'就結束拷貝。
strcpy
原型:extern char *strcpy(char *dest,char *src);
用法:#include <string.h>
功能:把src所指由NULL結束的字串複製到dest所指的數組中。
說明:src和dest所指記憶體地區不可以重疊且dest必須有足夠的空間來容納src的字串。
返回指向dest的指標。
memcpy
原型:extern void *memcpy(void *dest, void *src, unsigned int count);
用法:#include <string.h>
功能:由src所指記憶體地區複製count個位元組到dest所指記憶體地區。
說明:src和dest所指記憶體地區不能重疊,函數返回指向dest的指標。
memset
原型:extern void *memset(void *buffer, int c, int count);
用法:#include <string.h>
功能:把buffer所指記憶體地區的前count個位元組設定成字元c。
說明:返回指向buffer的指標。
原文:http://www.cppblog.com/gtwdaizi/articles/47764.html