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相當(dāng)于不變的字符串常量.
b)TPtr相當(dāng)與String類型。Tbuf相當(dāng)于char[]。前者與后者的唯一區(qū)別是,后者需要指定分配的??臻g大小。
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;
/* 數(shù)據(jù)類型轉(zhuǎn)換*/
TBuf 轉(zhuǎn)換為 TPtrC16
TBuf<32> tText(_L("2004/11/05 05:44:00"));
TPtrC16 tPtrSecond=tText.Mid(17,2);
TPtrC16 轉(zhuǎn)換為 TBufC16
TPtrC16 tPtrSecond=tText.Mid(17,2);
TBufC16<10> bufcs(tPtrSecond);
TBufC16 轉(zhuǎn)換為 TPtr16
TBufC16<10> bufcs(tPtrSecond);
TPtr16 f=bufcs.Des();
TPtr16 轉(zhuǎn)換為 TBuf
TBuf<10> bufSecond;
bufSecond.Copy(f);
TBuf 轉(zhuǎn)換為 TPtr16
TBuf<10> bufSecond(_L("abc"));
TPtr16 f;
f.Copy(bufSecond);
TBuf 轉(zhuǎn)換為 TInt
TInt aSecond;
TLex iLexS(bufSecond);
iLexS.Val(aSecond);
TInt 轉(zhuǎn)換為 TBuf
TBuf<32> tbuf;
TInt i=200;
tbuf.Num(i);
1.串轉(zhuǎn)換成數(shù)字
TBuf16<20> buf(_L( "123" ) );
TLex lex( buf );
TInt iNum;
lex.Val( iNum );
2.數(shù)字轉(zhuǎn)換成串
TBuf16<20> buf;
TInt iNum = 20;
buf.Format( _L( "%d" ) , iNum );
3.將symbian串轉(zhuǎn)換成char串
char* p = NULL;
TBuf8<20> buf( _L( "aaaaa" ) );
p = (char *)buf.Ptr();
4.UTF-8轉(zhuǎn)換成UNICODE
CnvUtfConverter::ConvertToUnicodeFromUtf8( iBuf16 , iBuf8 );
5.UNICODE轉(zhuǎn)換成UTF-8
CnvUtfConverter::ConvertFromUnicodeToUtf8( iBuf8 , iBuf16 );
6.將char串轉(zhuǎn)換成symbian串
char* cc = "aaaa";
TPtrC8 a;
a.Set( (const TUint8*)cc , strlen(cc) );
7、將TPtrc8與TPtrc16之間轉(zhuǎn)化
// 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主要應(yīng)用是初始化某個內(nèi)存空間。用來對一段內(nèi)存空間全部設(shè)置為某個字符。
memcpy是用于COPY源空間的數(shù)據(jù)到目的空間中,用來做內(nèi)存拷貝可以拿它拷貝任何數(shù)據(jù)類型的對象。
strcpy只能拷貝字符串了,它遇到'\0'就結(jié)束拷貝。
strcpy
原型:extern char *strcpy(char *dest,char *src);
用法:#include <string.h>
功能:把src所指由NULL結(jié)束的字符串復(fù)制到dest所指的數(shù)組中。
說明:src和dest所指內(nèi)存區(qū)域不可以重疊且dest必須有足夠的空間來容納src的字符串。
返回指向dest的指針。
memcpy
原型:extern void *memcpy(void *dest, void *src, unsigned int count);
用法:#include <string.h>
功能:由src所指內(nèi)存區(qū)域復(fù)制count個字節(jié)到dest所指內(nèi)存區(qū)域。
說明:src和dest所指內(nèi)存區(qū)域不能重疊,函數(shù)返回指向dest的指針。
memset
原型:extern void *memset(void *buffer, int c, int count);
用法:#include <string.h>
功能:把buffer所指內(nèi)存區(qū)域的前count個字節(jié)設(shè)置成字符c。
說明:返回指向buffer的指針。