随笔 - 2, 文章 - 73, 评论 - 60, 引用 - 0
数据加载中……

[S60]symbian基本类型转换

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

posted on 2008-04-21 21:17 郭天文 阅读(2410) 评论(0)  编辑 收藏 引用 所属分类: S60


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理