MultiByteToWideChar 和 WideCharToMultiByte 中第1个参数的理解

本文详细解释了MultiByteToWideChar和WideCharToMultiByte函数的使用方法,特别是第一个参数的作用,即指定转换所使用的字符集(如ANSI或UTF-8),并通过具体示例展示了如何在不同字符集间进行转换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

MultiByteToWideChar 和 WideCharToMultiByte 中第1个参数的理解

MSDN说:Specifies the code page to be used to perform the conversion,

我的理解是:这2个都是多字符集中的某一个(如ansi或是utf-8)转成宽字符(unicode),

如:

int   len=MultiByteToWideChar(CP_UTF8,0,str,-1,0,0);//请求WCHAR字符数
以上是代表“多字符集”转成“宽字符,这个多字符为UTF-8 ,好理解吧?呵呵

 

::MultiByteToWideChar(CP_ACP, NULL, szAnsi, strlen(szAnsi), wszString, wcsLen);
以上是 将 多字符集转成宽字符,,这个多字符集中的哪一个呢?CP_ACP来告诉你,是ansi。

 

 ::WideCharToMultiByte(CP_ACP, NULL, wszString, wcslen(wszString), szAnsi, ansiLen, NULL, NULL);
意思是:将宽字符 转成 多字符集中的ansi

 

::WideCharToMultiByte(CP_UTF8, NULL, wszString, wcslen(wszString), szU8, u8Len, NULL, NULL);
意思是:将宽字符 转成 字符集中的UFT-8码

特此记录,不混乱!

Option Explicit Public Declare PtrSafe Function MultiByteToWideChar Lib "kernel32" ( _ ByVal CodePage As Long, _ ByVal dwFlags As Long, _ ByRef lpMultiByteStr As Any, _ ByVal cchMultiByte As Long, _ ByVal lpWideCharStr As Long, _ ByVal cchWideChar As Long) As Long Public Declare PtrSafe Function WideCharToMultiByte Lib "kernel32" ( _ ByVal CodePage As Long, _ ByVal dwFlags As Long, _ ByVal lpWideCharStr As Long, _ ByVal cchWideChar As Long, _ ByRef lpMultiByteStr As Any, _ ByVal cchMultiByte As Long, _ ByVal lpDefaultChar As String, _ ByVal lpUsedDefaultChar As Long) As Long Public Const CP_UTF8 = 65001 ' ���A�����{�ʐiUTF8�i���I���{���� ' �A�� ' strInput�F���{������ ' strFile�F�ۑ��IUTF8�i�������H�a ' bBOM�FTrue�\��������"EFBBBF"���CFalse�\���s�� Public Function WriteUTF8File(strInput As String, strFile As String, Optional bBOM As Boolean = True) As Boolean Dim bByte As Byte Dim ReturnByte() As Byte Dim lngBufferSize As Long Dim lngResult As Long Dim TLen As Long ' ���f�A�����������ۈ׋� If Len(strInput) = 0 Then Exit Function On Error GoTo errHandle ' ���f�������ۑ��݁C�@���ݑ��폜 If Dir(strFile) <> "" Then Kill strFile TLen = Len(strInput) lngBufferSize = TLen * 3 + 1 ReDim ReturnByte(lngBufferSize - 1) lngResult = WideCharToMultiByte(CP_UTF8, 0, StrPtr(strInput), TLen, _ ReturnByte(0), lngBufferSize, vbNullString, 0) If lngResult Then lngResult = lngResult - 1 ReDim Preserve ReturnByte(lngResult) Open strFile For Binary As #1 If bBOM = True Then bByte = 239 Put #1, , bByte bByte = 187 Put #1, , bByte bByte = 191 Put #1, , bByte End If Put #1, , ReturnByte Close #1 End If WriteUTF8File = True Exit Function errHandle: WriteUTF8File = False MsgBox Err.Description, , "���� - " & Err.Number End Functions这段Excel代码实行到Public Function WriteUTF8File(strInput As String, strFile As String, Optional bBOM As Boolean = True) As Boolean的时候报编译错误,类型不匹配
最新发布
04-01
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值