多字节、宽字节转换代码--MutiByteToWideChar & WideCharToMutiByte

/************************************************************************/
/* Project:    宽字节与多字节互相转换                                        */
/* Author:    LandyTan                                                    */
/* Time:    2017/11/04                                                    */
/************************************************************************/


#include <iostream>
#include <Windows.h>
using namespace std;


char* _WTA(__in wchar_t* pszInBufBuf, __in int nInSize, __out char** pszOutBuf, __out int* pnOutSize);
wchar_t* _ATW(__in char* pszInBuf, __in int nInSize, __out wchar_t** pszOutBuf, __out int* pnOutSize);

HANDLE hFileA1,hFileW2;
DWORD dwWriteSize1,dwWriteSize2;

int main()
{
    hFileA1 = CreateFile(L"C:\\Users\\Public\\Music\\acode.txt",GENERIC_WRITE,FILE_SHARE_WRITE,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
    hFileW2 = CreateFile(L"C:\\Users\\Public\\Music\\wcode.txt",GENERIC_WRITE,FILE_SHARE_WRITE,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);

    {
        char* pszIn = "大利嘉";
        wchar_t* pszOut = NULL;
        int nOutSize = 0;
        if (!_ATW(pszIn, strlen(pszIn), &pszOut, &nOutSize))
            cerr << "多字节转宽字节失败" << endl;

        else cout << "多字节:" << pszIn << "\t宽字节:" << pszOut << endl;



        delete[] pszOut;
        pszOut = NULL;
    }

    {
        wchar_t* pszIn = L"大利嘉";
        char* pszOut = NULL;
        int nOutSize = 0;
        if (!_WTA(pszIn, wcslen(pszIn), &pszOut, &nOutSize))
            cerr << "宽字节转多字节失败" << endl;
        else cout << "宽字节:" << pszIn << "\t多字节:" << pszOut << endl;


        delete[] pszOut;
        pszOut = NULL;
    }
    CloseHandle(hFileA1);
    CloseHandle(hFileW2);
    system("pause");
    return 0;
}


/************************************************************************/
/* Name:        _ATW                                                    */
/* Function:    多字节转宽字节                                            */
/* Parameter list:                                                        */
/*                pszInBuf        被转换的字符串                            */
/*                nInSize            字符串长度                                */
/*                pszOutBuf        接收转换的字符串                            */
/*                pnOutSize        接收字符串的长度                            */
/************************************************************************/
wchar_t* _ATW(__in char* pszInBuf, __in int nInSize, __out wchar_t** pszOutBuf, __out int* pnOutSize)
{
    if(!pszInBuf || !pszOutBuf || !pnOutSize || nInSize <= 0)return NULL;
    *pnOutSize = MultiByteToWideChar(NULL, NULL, pszInBuf, nInSize, *pszOutBuf, 0);// 获取待转换字符串的缓冲区所需大小
    if(*pnOutSize == 0)return NULL;
    (*pnOutSize)++;
    *pszOutBuf = new wchar_t[*pnOutSize];
    memset((void*)*pszOutBuf, 0, sizeof(wchar_t) * (*pnOutSize));
    if(MultiByteToWideChar(NULL, NULL, pszInBuf, nInSize, *pszOutBuf, *pnOutSize) == 0)
        return NULL;
    else 
    {
        char mb[1024];
        WideCharToMultiByte(CP_UTF8, 0, *pszOutBuf, *pnOutSize, mb, 1024, NULL, NULL);

        int ret = WriteFile(hFileW2,mb,strlen(mb),&dwWriteSize2,NULL);
        printf("GetError: ret:%d  --- err:%d\n",ret,GetLastError());    
        return *pszOutBuf;
    }
}


/************************************************************************/
/* Name:        _WTA                                                    */
/* Function:    宽字节转多字节                                            */
/* Parameter list:                                                        */
/*                pszInBuf        被转换的字符串                            */
/*                nInSize            字符串长度                                */
/*                pszOutBuf        接收转换的字符串                            */
/*                pnOutSize        接收字符串的长度                            */
/************************************************************************/
char* _WTA(__in wchar_t* pszInBuf, __in int nInSize, __out char** pszOutBuf, __out int* pnOutSize)
{
    if(!pszInBuf || !pszOutBuf || !pnOutSize || nInSize <= 0)return NULL;
    *pnOutSize = WideCharToMultiByte(NULL, NULL, pszInBuf, nInSize, *pszOutBuf, 0, NULL, NULL);// 获取待转换字符串的缓冲区所需大小
    if(*pnOutSize == 0)return NULL;
    (*pnOutSize)++;
    *pszOutBuf = new char[*pnOutSize];
    memset((void*)*pszOutBuf, 0, sizeof(char)* (*pnOutSize));
    if (WideCharToMultiByte(NULL, NULL, pszInBuf, nInSize, *pszOutBuf, *pnOutSize, NULL, NULL) == 0) // 这里才是转换
        return NULL;

    else 
    {
        int ret = WriteFile(hFileA1,*pszOutBuf,*pnOutSize,&dwWriteSize1,NULL);
        printf("GetError: ret:%d  --- err:%d\n",ret,GetLastError());    
        return *pszOutBuf;
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值