Advertisement
Guest User

Untitled

a guest
Jan 19th, 2020
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.66 KB | None | 0 0
  1. //unicode.h
  2. #ifndef __UNICODE_H
  3. #define __UNICODE_H
  4.  
  5. #include "platform.h"
  6. #ifdef _WINDOWS
  7.  
  8. wchar_t* convert_to_unicode(DWORD cp,const char* buf,int bufLen = -1);
  9. char* convert_from_unicode(DWORD cp,const wchar_t* buf,int nLen = -1);
  10.  
  11. char* encoding_convert(DWORD from_cp,DWORD to_cp,const char* buf,int bufLen = -1);
  12.  
  13. #define ansi_to_utf16(ansi) convert_to_unicode(CP_ACP,ansi)
  14. #define utf16_to_ansi(utf16) convert_from_unicode(CP_ACP,utf16)
  15. #define utf8_to_utf16(utf8) convert_to_unicode(CP_UTF8,utf8)
  16. #define utf16_to_utf8(utf16) convert_from_unicode(CP_UTF8,utf16)
  17. #define utf8_to_ansi(utf8) encoding_convert(CP_UTF8,CP_ACP,utf8)
  18.  
  19. #endif
  20. #endif
  21.  
  22. //unicode.cpp
  23. #include "unicode.h"
  24.  
  25. #ifdef _WINDOWS
  26.  
  27. wchar_t* convert_to_unicode(DWORD cp,const char* buf,int bufLen)
  28. {
  29.     if(bufLen == -1) bufLen = strlen(buf)+1;
  30.     DWORD outLen = MultiByteToWideChar(cp,0,buf,bufLen,NULL,0);
  31.     wchar_t* out = new wchar_t[outLen];
  32.     MultiByteToWideChar(cp,0,buf,bufLen,out,outLen);
  33.     return out;
  34. }
  35.  
  36. char* convert_from_unicode(DWORD cp,const wchar_t* buf,int nLen)
  37. {
  38.     if(nLen == -1) nLen = wcslen(buf)+1;
  39.     DWORD cbOutLen = WideCharToMultiByte(cp,0,buf,nLen,NULL,0,NULL,NULL);
  40.     char* out = new char[cbOutLen];
  41.     WideCharToMultiByte(cp,0,buf,nLen,out,cbOutLen,NULL,NULL);
  42.     return out;
  43. }
  44.  
  45. char* encoding_convert(DWORD from_cp,DWORD to_cp,
  46.     const char* buf,int bufLen)
  47. {
  48.     if(from_cp == to_cp)
  49.     {
  50.         if(bufLen == -1) bufLen = strlen(buf)+1;
  51.         char* out = new char[bufLen];
  52.         memcpy(out,buf,bufLen);
  53.         return out;
  54.     }
  55.  
  56.     wchar_t* pNewBuf = convert_to_unicode(from_cp,buf,bufLen);
  57.     char* pOut = convert_from_unicode(to_cp,pNewBuf);
  58.     delete[] pNewBuf;
  59.     return pOut;
  60. }
  61.  
  62. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement