road420

导航

<2007年7月>
24252627282930
1234567
891011121314
15161718192021
22232425262728
2930311234

统计

常用链接

留言簿(2)

随笔档案

文章档案

搜索

最新评论

阅读排行榜

评论排行榜

字符类型数据转换

1/**///////////////////////////////////////////////////////////////////////////
  2// Module: stringutility.h
  3// Conversion among wchar_t, char and TCHAR
  4//
  5/**///////////////////////////////////////////////////////////////////////////
  6#ifndef _STRINGUTILITY_H_
  7#define _STRINGUTILITY_H_
  8
  9/**///////////////////////////////////////////////////////////////////////////
 10// get PSTR by PWSTR
 11// This function malloc some memory and you must free it
 12inline char* newPSTRFromPWSTRT(PWSTR source)
 13{
 14    char *pCh = NULL;
 15    int nLength = 0;
 16    if(!source)
 17    {
 18        pCh = new char[1];
 19    }

 20    else
 21    {
 22        nLength= wcslen(source);
 23        pCh = new char[2 * nLength + 1];
 24        WideCharToMultiByte(CP_ACP,
 25            0,
 26            source,
 27            -1,
 28            pCh,
 29            nLength * 2,
 30            NULL,
 31            NULL);
 32    }

 33    pCh[2 * nLength] = '\0';
 34    return pCh;
 35}

 36
 37/**///////////////////////////////////////////////////////////////////////////
 38// get PWSTR by PSTR
 39// This function malloc some memory and you must free it
 40inline wchar_t* newPWSTRFromPSTR(PSTR source)
 41{
 42    wchar_t *pCh = NULL;
 43    int nLength = 0;
 44    if(!source)
 45    {
 46        pCh = new wchar_t[1];
 47    }

 48    else
 49    {
 50        nLength = strlen(source);
 51        pCh = new wchar_t[nLength + 1];
 52        MultiByteToWideChar(CP_ACP,
 53            0,
 54            source,
 55            -1,
 56            pCh,
 57            nLength);
 58    }

 59    pCh[nLength] = '\0';
 60    return pCh;
 61}

 62
 63/**///////////////////////////////////////////////////////////////////////////
 64// get PSTR by PTSTR
 65// This function malloc some memory and you must free it
 66
 67inline char* newPSTRFromPTSTR(PTSTR    source)
 68{
 69#ifdef UNICODE
 70    return newPSTRFromPWSTRT(source);
 71#else
 72    char* pCh;
 73    int iLength = 0;
 74    if(!source)
 75    {
 76        pCh = new char[1];
 77    }

 78    else
 79    {
 80        iLength = strlen(source);
 81        pCh = new char[iLength +1];
 82        strcpy(pCh, source);
 83    }

 84    pCh[iLength] = '\0';
 85    return pCh;
 86#endif
 87}

 88
 89/**///////////////////////////////////////////////////////////////////////////
 90// get PWSTR by PTSTR
 91// This function malloc some memory and you must free it
 92
 93inline wchar_t* newPWSTRFromPTSTR(PTSTR source)
 94{
 95#ifdef UNICODE
 96    wchar_t *pCh = NULL;
 97    int nLength = 0;
 98    if(!source)
 99    {
100        pCh = new wchar_t[1];
101    }

102    else
103    {
104        nLength = wcslen(source);
105        pCh = new wchar_t[nLength + 1];
106        wcscpy(pCh, source);
107    }

108    pCh[nLength] = L'\0';
109    return pCh;
110#else
111    return newPWSTRFromPTSTR(source);
112#endif
113}

114
115
116/**///////////////////////////////////////////////////////////////////////////
117// get PTSTR by PSTR
118// This function malloc some memory and you must free it
119inline TCHAR* newPTSTRFromPSTR(PSTR source)
120{
121#ifdef UNICODE
122    return newPWSTRFromPSTR(source);
123#else
124    return newPSTRFromPTSTR(source);
125#endif
126}

127
128/**///////////////////////////////////////////////////////////////////////////
129// get PTSTR by PWSTR
130// This function malloc some memory and you must free it
131inline TCHAR* newPTSTRFromPWSTR(PWSTR source)
132{
133#ifdef UNICODE
134    return newPWSTRFromPTSTR(source);
135#else
136    return newPSTRFromPWSTRT(source);
137#endif
138}

139#endif // end of #ifndef _STRINGUTILITY_H_

posted on 2008-07-15 08:49 深邃者 阅读(140) 评论(0)  编辑 收藏 引用


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