【问题标题】:_bstr_t to UTF-8 possible?_bstr_t 到 UTF-8 可能吗?
【发布时间】:2010-10-12 04:20:58
【问题描述】:

我有一个包含日文文本的_bstr_t 字符串。我想将此字符串转换为定义为char * 的UTF-8 字符串。

我可以将_bstr_t 字符串转换为char * (UTF-8) 字符串而不会丢失日文字符吗?

【问题讨论】:

    标签: c++ visual-c++ com atl bstr


    【解决方案1】:

    使用 WideCharToMultiByte() – 传递 CP_UTF8 作为第一个参数。

    请注意,BSTR 可以是空指针并且对应于空字符串 - 将此视为特殊情况。

    【讨论】:

      【解决方案2】:

      这里是一些应该进行转换的代码。

      void PrintUtf8(const TCHAR* value) { 
          if (value == nullptr) {
              printf("");
              return;
          }
          int n = WideCharToMultiByte(CP_UTF8, 0, value, -1, nullptr, 0, nullptr, nullptr);
          if (n <= 0) {
              printf("");
              return;
          }
          char* buffer = new char[n];
          WideCharToMultiByte(CP_UTF8, 0, value, -1, buffer, n, nullptr, nullptr);
          printf("%s", buffer);
          delete(buffer);
      }
      

      【讨论】:

        【解决方案3】:

        这类事情非常方便的 MSDN 参考:http://msdn.microsoft.com/en-us/library/ms235631(VS.80).aspx

        我认为您需要转到 wchar_t*,因为 char* 会丢失 Unicode 内容,尽管我不确定。

        // convert_from_bstr_t.cpp
        // compile with: /clr /link comsuppw.lib
        
        #include <iostream>
        #include <stdlib.h>
        #include <string>
        
        #include "atlbase.h"
        #include "atlstr.h"
        #include "comutil.h"
        
        using namespace std;
        using namespace System;
        
        int main()
        {
            _bstr_t orig("Hello, World!");
            wcout << orig << " (_bstr_t)" << endl;
        
            // Convert to a char*
            const size_t newsize = 100;
            char nstring[newsize];
            strcpy_s(nstring, (char *)orig);
            strcat_s(nstring, " (char *)");
            cout << nstring << endl;
        
            // Convert to a wchar_t*
            wchar_t wcstring[newsize];
            wcscpy_s(wcstring, (wchar_t *)orig);
            wcscat_s(wcstring, L" (wchar_t *)");
            wcout << wcstring << endl;
        
            // Convert to a CComBSTR
            CComBSTR ccombstr((char *)orig);
            if (ccombstr.Append(L" (CComBSTR)") == S_OK)
            {
                CW2A printstr(ccombstr);
                cout << printstr << endl;
            }
        
            // Convert to a CString
            CString cstring((char *)orig);
            cstring += " (CString)";
            cout << cstring << endl;
        
            // Convert to a basic_string
            string basicstring((char *)orig);
            basicstring += " (basic_string)";
            cout << basicstring << endl;
        
            // Convert to a System::String
            String ^systemstring = gcnew String((char *)orig);
            systemstring += " (System::String)";
            Console::WriteLine("{0}", systemstring);
            delete systemstring;
        }
        

        【讨论】:

        • 感谢您的回复尼克。问题是我想通过 Windows 套接字发送这个 _bstr_t 内容,它只允许发送 char* 类型(请检查 ws2def.h 文件中的 WSABUF 结构)。现在 wchat 不会做。 _WSABUF 结构是否有宽字符版本?
        • Windows 套接字不关心您发送什么数据。在这种情况下,您只需将 reinterpret_cast 重新解释为 char* 就可以了。
        • 只是不要弄乱字节数 - 它是 Unicode 字符数乘以 sizeof(WCHAR) - 并且使用空 BSTR。
        • 虽然 Windows Sockets 不关心发送什么数据,但如果目的地需要理解数据并且使用不同的字节顺序,最好使用 UTF-8。尤其是在使用两种字节顺序的系统的混合环境中。
        猜你喜欢
        • 2011-06-30
        • 2011-05-23
        • 2014-04-16
        • 2015-12-06
        • 2012-02-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多