Symbian 基本類型轉換

原文出處:http://www.cppblog.com/gtwdaizi/articles/47764.html

 

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的指針。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章