C + + convert Unicode utf-8 gb2312 encoding

Source: Internet
Author: User

The conversion method for Unicode, Utf-8, gb2312 Three encoding formats using vc++6.0 in the Windows Development environment:

[CPP]View Plaincopy
  1. #include <iostream>
  2. #include <string>
  3. #include <Windows.h>
  4. Using namespace std;
  5. void UnicodeToUTF8 (const wstring &SRC, string& result)
  6. {
  7. int n = widechartomultibyte (Cp_utf8, 0, Src.c_str (),-1, 0, 0, 0, 0);
  8. Result.resize (n);
  9. :: WideCharToMultiByte (Cp_utf8, 0, Src.c_str (),-1, (char*) result.c_str (), Result.length (), 0, 0);
  10. }
  11. void unicodeToGB2312 (const wstring& WSTR, string& result)
  12. {
  13. int n = widechartomultibyte (CP_ACP, 0, Wstr.c_str (),-1, 0, 0, 0, 0);
  14. Result.resize (n);
  15. :: WideCharToMultiByte (CP_ACP, 0, Wstr.c_str (),-1, (char*) result.c_str (), n, 0, 0);
  16. }
  17. void Utf8tounicode (const string& SRC, wstring& result)
  18. {
  19. int n = MultiByteToWideChar (Cp_utf8, 0, Src.c_str (),-1, NULL, 0);
  20. Result.resize (n);
  21. :: MultiByteToWideChar (Cp_utf8, 0, Src.c_str (),-1, (lpwstr) result.c_str (), result.length ());
  22. }
  23. void Gb2312tounicode (const string& SRC, wstring& result)
  24. {
  25. int n = MultiByteToWideChar (CP_ACP, 0, Src.c_str (),-1, NULL, 0);
  26. Result.resize (n);
  27. :: MultiByteToWideChar (CP_ACP, 0, Src.c_str (),-1, (lpwstr) result.c_str (), result.length ());
  28. }
  29. void Printbyte (String str)
  30. {
  31. int i=0;
  32. For (i=0; i<str.length (); i++)
  33. {
  34. printf ("%x", (unsigned char) str.at (i));
  35. }
  36. printf ("\ n");
  37. }
  38. void Wprintbyte (wstring str)
  39. {
  40. int i=0;
  41. For (i=0; I<str.length () *sizeof (wchar_t); i++)
  42. {
  43. printf ("%x", * ((unsigned char*) str.c_str () +i));
  44. }
  45. printf ("\ n");
  46. }
  47. int main ()
  48. {
  49. String strText = "ab kanji";
  50. String strUTF8;
  51. Wstring Wstrunicode;
  52. String strGB2312;
  53. Gb2312tounicode (StrText, Wstrunicode);
  54. printf ("unicode=");
  55. Wprintbyte (Wstrunicode);
  56. UnicodeToUTF8 (Wstrunicode, strUTF8);
  57. printf ("UTF-8 =");
  58. Printbyte (STRUTF8);
  59. Utf8tounicode (Strutf8,wstrunicode);
  60. printf ("unicode=");
  61. Wprintbyte (Wstrunicode);
  62. unicodeToGB2312 (wstrunicode,strgb2312);
  63. printf ("GB2312 =");
  64. Printbyte (strGB2312);
  65. return 0;
  66. }

This is a string of "ab Kanji" for testing, and its ASCII code is a BA ba D7 D6
Output Result:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.