Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //unicode.h
- #ifndef __UNICODE_H
- #define __UNICODE_H
- #include "platform.h"
- #ifdef _WINDOWS
- wchar_t* convert_to_unicode(DWORD cp,const char* buf,int bufLen = -1);
- char* convert_from_unicode(DWORD cp,const wchar_t* buf,int nLen = -1);
- char* encoding_convert(DWORD from_cp,DWORD to_cp,const char* buf,int bufLen = -1);
- #define ansi_to_utf16(ansi) convert_to_unicode(CP_ACP,ansi)
- #define utf16_to_ansi(utf16) convert_from_unicode(CP_ACP,utf16)
- #define utf8_to_utf16(utf8) convert_to_unicode(CP_UTF8,utf8)
- #define utf16_to_utf8(utf16) convert_from_unicode(CP_UTF8,utf16)
- #define utf8_to_ansi(utf8) encoding_convert(CP_UTF8,CP_ACP,utf8)
- #endif
- #endif
- //unicode.cpp
- #include "unicode.h"
- #ifdef _WINDOWS
- wchar_t* convert_to_unicode(DWORD cp,const char* buf,int bufLen)
- {
- if(bufLen == -1) bufLen = strlen(buf)+1;
- DWORD outLen = MultiByteToWideChar(cp,0,buf,bufLen,NULL,0);
- wchar_t* out = new wchar_t[outLen];
- MultiByteToWideChar(cp,0,buf,bufLen,out,outLen);
- return out;
- }
- char* convert_from_unicode(DWORD cp,const wchar_t* buf,int nLen)
- {
- if(nLen == -1) nLen = wcslen(buf)+1;
- DWORD cbOutLen = WideCharToMultiByte(cp,0,buf,nLen,NULL,0,NULL,NULL);
- char* out = new char[cbOutLen];
- WideCharToMultiByte(cp,0,buf,nLen,out,cbOutLen,NULL,NULL);
- return out;
- }
- char* encoding_convert(DWORD from_cp,DWORD to_cp,
- const char* buf,int bufLen)
- {
- if(from_cp == to_cp)
- {
- if(bufLen == -1) bufLen = strlen(buf)+1;
- char* out = new char[bufLen];
- memcpy(out,buf,bufLen);
- return out;
- }
- wchar_t* pNewBuf = convert_to_unicode(from_cp,buf,bufLen);
- char* pOut = convert_from_unicode(to_cp,pNewBuf);
- delete[] pNewBuf;
- return pOut;
- }
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement