Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1469499
  • 博文数量: 218
  • 博客积分: 6394
  • 博客等级: 准将
  • 技术积分: 2563
  • 用 户 组: 普通用户
  • 注册时间: 2008-02-08 15:33
个人简介

持之以恒

文章分类

全部博文(218)

文章存档

2013年(8)

2012年(2)

2011年(21)

2010年(55)

2009年(116)

2008年(16)

分类: WINDOWS

2009-07-09 11:29:47

在进行计算机网络传输的时候,如果要传输相应的宽字符(比如说:汉字),必须将其转化为字符类型,再在接收的时候将其转化为宽字符
相关的函数如下:
WideCharToMultiByte
MSDN上的解释:

Maps a UTF-16 (wide character) string to a new character string. The new character string is not necessarily from a multibyte character set.

也就是将相应的宽字符集(UNICODE)转化为ACSII字符

他的具体的格式如下:
int WideCharToMultiByte
{  
    __in UINT CodePage;
    __in DWORD  dwFlags;
    __in LPCWSTR lpWideCharStr;
    __in int cchWIdeChar;
    __out LPSTR lpMutiByteStr;
    __in int cbMultiByte;
    __in LPCSTR lpDefaultChar;
    __out LPBOOL lpUsedDefaultChar;
}
一共八个参数,下面是参数说明:
  CodePage [in] Code page to use in performing the conversion. This parameter can be set to the value of any code page that is installed or available in the operating system.
Value Meaning
CP_ACP

The system default Windows ANSI code page.

Note  This value can be different on different computers, even on the same network. It can be changed on the same computer, leading to stored data becoming irrecoverably corrupted. This value is only intended for temporary use and permanent storage should use UTF-16 or UTF-8 if possible.

CP_MACCP

The current system Macintosh code page.

Note  This value can be different on different computers, even on the same network. It can be changed on the same computer, leading to stored data becoming irrecoverably corrupted. This value is only intended for temporary use and permanent storage should use UTF-16 or UTF-8 if possible.

Note   This value is used primarily in legacy code and should not generally be needed since modern Macintosh computers use Unicode for encoding.

CP_OEMCP

The current system OEM code page.

Note  This value can be different on different computers, even on the same network. It can be changed on the same computer, leading to stored data becoming irrecoverably corrupted. This value is only intended for temporary use and permanent storage should use UTF-16 or UTF-8 if possible.

CP_SYMBOL

Windows 2000: Symbol code page (42).

CP_THREAD_ACP

Windows 2000: The Windows ANSI code page for the current thread.

Note  This value can be different on different computers, even on the same network. It can be changed on the same computer, leading to stored data becoming irrecoverably corrupted. This value is only intended for temporary use and permanent storage should use UTF-16 or UTF-8 if possible.

CP_UTF7

Windows Me/98, Windows NT 4.0: UTF-7. Use this value only when forced by a 7-bit transport mechanism. Use of UTF-8 is preferred. With this value set, lpDefaultChar and lpUsedDefaultChar must be set to NULL.

CP_UTF8

Windows Me/98, Windows NT 4.0: UTF-8. With this value set, lpDefaultChar and lpUsedDefaultChar must be set to NULL.

我们一般采用的是CP_ACP

dwFlags [in] Flags indicating the conversion type.
        这个一般我们设置成0
lpWideCharStr [in]

Pointer to the Unicode string to convert.(带转化的相应的宽字符)

cchWideChar [in]

Size, in characters, of the string indicated by lpWideCharStr. If this parameter is set to -1, the function assumes the string to be null-terminated and calculates the length automatically, including the terminating null character. If cchWideChar is set to 0, the function fails.

注意这个我们通常将其设置成-1
lpMultiByteStr [out]

Optional. Pointer to a buffer that receives the converted string.

输出的字符串的缓存
cbMultiByte [in]

Size, in bytes, of the buffer indicated by lpMultiByteStr. If this parameter is set to 0, the function returns the required buffer size for lpMultiByteStr and makes no use of the output parameter itself.

这个我们通常设置成相应的lpMutiByteStr对应的长度
lpDefaultChar [in]
The application sets this parameter to NULL if the function is to use a system default value.
这个我们一般将其设置成空(NULL)

lpUsedDefaultChar [out]
一般这个参数设置成FALSE
=================================================================================
以上就是上面的8个参数的相关的含义

关于这个函数的返回值:
Returns the number of bytes written to the buffer pointed to by lpMultiByteStr if successful. The number includes the byte for the terminating null character. If the function succeeds and cbMultiByte is 0, the return value is the required size, in bytes, for the buffer indicated by lpMultiByteStr.

================================================================================
下面是一个例子:
char inBuff[256];
TCHAR w_inbuf[256] = _T("Hello,大家好,我是宽字符,我来了");
::WideCharToMutiByte(CP_ACP,0,(LPCWSTR)w_inbuf,-1,(LPSTR)inBuff,256,NULL,FALSE);
通过这个函数就将相应的宽字符转化成ASCII字符了

同理:将相应的ASCII字符转化成宽字符也有相应的方法
MutiByteToWideChar
int MultiByteToWideChar
(
    __in UINT CodePage;
   __in DWORD dwFlags;
   __in LPCSTR lpMutiByteStr;
   __in int cchMutiByte;
   __out LPWSTR lpWideCharStr;
   __in int cchWideChar;
}
这个参数比较少,6个,具体的就不解释了!
另外一个重要的网址是:
http://www.cppblog.com/sunraiing9/archive/2007/03/21/20281.html
自己写的将宽字符函数转化成对应的窄字符的函数
:主要是可以先通过WideCharToMultiByte知道转化后的窄字符字符串的大小,以便于进行动态分配,防止浪费空间。
char* myWideCharToMultiByte(LPCWSTR lpWideCharStr)
{
    DWORD dwNum;
    dwNum = WideCharToMultiByte(CP_OEMCP,NULL,lpWideCharStr,-1,NULL,0,NULL,FALSE);
    char *returnText;
    returnText = new char[dwNum];
    if(!returnText)
    {
        delete []returnText;
    }
    WideCharToMultiByte (CP_OEMCP,NULL,lpWideCharStr,-1,returnText,dwNum,NULL,FALSE);
    return returnText;
}

阅读(1723) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~