C++ AsciiUnicodeUtf8
2022-12-02 / C/C++ / 458 次围观 / 0 次吐槽 /AsciiUnicodeUtf8.h:
#pragma once DWORD BytesToHexStr(IN BYTE*nbData,DWORD nbDataSize,OUT char*szpHexBuf); //字节集转十六进制字串 DWORD AnsiToUtf8(LPCSTR Ansi,OUT LPSTR szpUtf8Buf); char* UniCodeToAscii(IN WCHAR*szpUnicodeStr,OUT char*szpAsciiStr); char* utf8ToAscii(IN char*szpUtf8/*要转换的utf8字符串*/,OUT char*szpAscii/*转换后的多字节字符串*/); //转码函数
AsciiUnicodeUtf8.cpp:
#include <pch.h> #include <Windows.h> #include "AsciiUnicodeUtf8.h" DWORD BytesToHexStr(IN BYTE*nbData,DWORD nbDataSize,OUT char*szpHexBuf); //字节集转十六进制字串 DWORD AnsiToUtf8(LPCSTR Ansi,OUT LPSTR szpUtf8Buf); char* UniCodeToAscii(IN WCHAR*szpUnicodeStr,OUT char*szpAsciiStr); WCHAR* utf8ToUnicode(IN char*szpUtf8 ,OUT WCHAR*szpUniCode); //注意 此函数 没检测 缓冲区大小 char* utf8ToAscii(IN char*szpUtf8,OUT char*szpAscii); BOOL CharToHex(OUT char*szpBufHex,BYTE c); DWORD AnsiToUtf8(LPCSTR Ansi,OUT LPSTR szpUtf8Buf) { int WLength = MultiByteToWideChar(CP_ACP, 0, Ansi, -1, NULL, 0); LPWSTR pszW = (LPWSTR) _alloca((WLength+1) * sizeof(WCHAR)); //Ansi字串转Unicode字串 MultiByteToWideChar(CP_ACP, 0, Ansi, -1, pszW, WLength); //计算UTF8转换所需要空间大小 int ALength = WideCharToMultiByte(CP_UTF8, 0, pszW, -1, NULL, 0, NULL, NULL); WideCharToMultiByte(CP_UTF8, 0, pszW, -1, szpUtf8Buf, ALength, NULL, NULL); szpUtf8Buf[ALength] = 0; szpUtf8Buf[ALength+1] = 0; return ALength; //返回UTF8字串长度 } char* UniCodeToAscii(IN WCHAR*szpUnicodeStr,OUT char*szpAsciiStr) { // 预算-缓冲区中多字节的长度 //CP_OEMCP int ansiiLen = WideCharToMultiByte(CP_ACP, 0,szpUnicodeStr, -1, nullptr, 0, nullptr, nullptr); // 给指向缓冲区的指针变量分配内存 // 把 szpUnicodeStr里存放的Unicode编码字串 转成ASCII 存放到 szpAsciiStr缓冲区 //CP_OEMCP WideCharToMultiByte(CP_ACP, 0, szpUnicodeStr, -1, szpAsciiStr, ansiiLen, nullptr, nullptr); return szpAsciiStr; } WCHAR* utf8ToUnicode(IN char*szpUtf8 ,OUT WCHAR*szpUniCode) { // 预算-缓冲区中宽字节的长度 //CP_UTF8 963 页码 int unicodeLen = MultiByteToWideChar(CP_UTF8, 0, szpUtf8, -1, nullptr, 0); // 给指向缓冲区的指针变量分配内存 WCHAR *pUnicode = (wchar_t*)malloc(sizeof(wchar_t)*unicodeLen); // 开始向缓冲区转换字节 //CP_UTF8 963页码 也可能 MultiByteToWideChar(CP_UTF8, 0, szpUtf8, -1, pUnicode, unicodeLen); wcscpy_s( szpUniCode,unicodeLen,pUnicode); free(pUnicode); return szpUniCode; } //注意 此函数 没检测 缓冲区大小 char* utf8ToAscii(IN char*szpUtf8,OUT char*szpAscii) { int iSizeUtf8 = MultiByteToWideChar(CP_UTF8, 0, szpUtf8, -1, nullptr, 0); WCHAR*putf8Buf=(WCHAR*)malloc(sizeof(WCHAR)*iSizeUtf8); //分配了内存 free utf8ToUnicode(szpUtf8,putf8Buf); UniCodeToAscii(putf8Buf,szpAscii); free(putf8Buf);//释放内存 return szpAscii; } BOOL CharToHex(OUT char*szpBufHex,BYTE c) { BYTE bLow=0; BYTE bHigh=0; bLow=c%16; //取低位0..15 bHigh=c/16;//取高位0..15 if (bHigh>9) //A,B,C,D,E,F { szpBufHex[0]='A'+bHigh-10; //'B' }else//0..9 { szpBufHex[0]='0'+bHigh; //'0'+2='2' } if (bLow>9) //A,B,C,D,E,F { szpBufHex[1]='A'+bLow-10; //'B' }else//0..9 { szpBufHex[1]='0'+bLow; //'0'+2='2' } szpBufHex[2]=0;//0 return TRUE; } DWORD BytesToHexStr(IN BYTE*nbData,DWORD nbDataSize,OUT char*szpHexBuf) //字节集转十六进制字串 { //1 //01 for (DWORD i=0;i<nbDataSize;i++) { //提高速度 //sprintf_s(&szpHexBuf[i*2],3,"%02X",nbData[i]); CharToHex(&szpHexBuf[i*2],nbData[i]); } return TRUE; }
- 上一篇:C++ MFC_Dll初始化窗口显示
- 下一篇:linux 修改文件用户组和所有者
Powered By Cheug's Blog
Copyright Cheug Rights Reserved.