symbian下wchar_t和HBufC间的转换
/********************此文可以被转发,但请转发者保留作者的署名权
****李浩
****msn:lihao_nx@hotmail.com
****
****email:lihaoyxj@gmail.com
****出处:lihaoyxj.cublog.cn
****from:http://www.cnblogs.com/lihaoyxj
*****************/
//head file:
const wchar_t* bufToNewWchar(const TDesC& aInput);
HBufC* wcharToNewBuf(const wchar_t* aInput);
//cpp file
wchar_t * wcsncpy (wchar_t *dst, const wchar_t *src, size_t count) {
memcpy(dst, src, count * sizeof(wchar_t));
return dst;
}
WCHAR* wstrdup(const WCHAR* s, size_t len)
{
if ( !s )
return NULL;
int l = (len==STRINGDUP_NOLEN)?wcslen(s):len;
WCHAR* news = new WCHAR[l+1];
wcsncpy(news, s, l);
news[l]=0;
return news;
}
const wchar_t* bufToNewWchar(const TDesC& aInput)
{
// This allocates a NEW wchar_t* buffer
wchar_t* ret = wstrdup((const wchar_t*)aInput.Ptr(), aInput.Length());
return (const wchar_t*)ret;
}
HBufC* wcharToNewBuf(const wchar_t* aInput)
{
int len = wcslen(aInput);
TUint16* wchars = (TUint16*)aInput;
RBuf16 buf16;
buf16.CreateL(len);
buf16.Copy(wchars);
return buf16.Alloc();
}
****李浩
****msn:lihao_nx@hotmail.com
****
****email:lihaoyxj@gmail.com
****出处:lihaoyxj.cublog.cn
****from:http://www.cnblogs.com/lihaoyxj
*****************/
//head file:
const wchar_t* bufToNewWchar(const TDesC& aInput);
HBufC* wcharToNewBuf(const wchar_t* aInput);
//cpp file
wchar_t * wcsncpy (wchar_t *dst, const wchar_t *src, size_t count) {
memcpy(dst, src, count * sizeof(wchar_t));
return dst;
}
WCHAR* wstrdup(const WCHAR* s, size_t len)
{
if ( !s )
return NULL;
int l = (len==STRINGDUP_NOLEN)?wcslen(s):len;
WCHAR* news = new WCHAR[l+1];
wcsncpy(news, s, l);
news[l]=0;
return news;
}
const wchar_t* bufToNewWchar(const TDesC& aInput)
{
// This allocates a NEW wchar_t* buffer
wchar_t* ret = wstrdup((const wchar_t*)aInput.Ptr(), aInput.Length());
return (const wchar_t*)ret;
}
HBufC* wcharToNewBuf(const wchar_t* aInput)
{
int len = wcslen(aInput);
TUint16* wchars = (TUint16*)aInput;
RBuf16 buf16;
buf16.CreateL(len);
buf16.Copy(wchars);
return buf16.Alloc();
}