0

我正在 Linux 上编写一个必须与现有 Windows 程序交互的程序。我无法修改 windows 程序的工作方式,但我必须与现有数据集成。该程序将通过 TCP 网络套接字接收原始数据结构。不幸的是,windows 程序在数据结构中嵌入了原始的多字节字符串,并且不指示正在使用哪个代码页。这适用于英语,但对于基于非拉丁语的语言(即:日语)则失败了。充其量,我可以猜测窗口正在使用的代码页。如果我正在运行并且我的语言环境设置为“ja”或“ja_JP”,我将不得不假设 Windows 机器正在使用“SHIFT-JS”代码页......丑陋,但这就是生活。

问题:

假设我在代码页上猜对了,如何将这些原始 MBCS 字符串转换为 UTF-8 字符串?

以下是原始数据的示例:

发送的字符串是:私のクラスへようこそ

从 windows (JP) 接收到的 MBCS 数据是(以字节为单位,添加了额外的“0x00”以确保空终止):

char kanji_win_raw_bytes[] =  { 0x8E, 0x84, 0x82, 0xCC, 0x83, 0x4E, 0x83, 0x89, 0x83, 0x58, 0x82, 0xD6, 0x82, 0xE6, 0x82, 0xA4, 0x82, 0xB1, 0x82, 0xBB, 0x00, 0x00, 0x00 };

据我所知,该字符串来自使用 SHIFT-JS 代码页的 Windows 机器。我试过 mbsrtowcs():

const char *ptr = (char*)m_data;
// m_data contains the byte array of MBCS data
if ( m_data != NULL )
{
    std::mbstate_t state = std::mbstate_t();

    size_t bufflen = std::mbsrtowcs(NULL, &ptr, 0, &state);
    if ( bufflen == (size_t)-1 )
    {
        std::cout << "ERROR! mbsrtowcs() " << strerror(errno) << std::endl;
        std::cout << "Error at: " <<  (int32_t)( (char*)ptr - (char*)m_data ) << std::endl;
        return;
    }

    std::vector<wchar_t> wstr(bufflen);
    std::cout << "converting " << bufflen << " characters" << std::endl;
    std::mbsrtowcs(&wstr[0], &ptr, wstr.size(), &state);
    std::wcout << "Wide string: " << &wstr[0] << std::endl
        << "The length, including '\\0': " << wstr.size() << std::endl;
}

对 mbsrtowcs() 的调用在位置“0”处失败,没有转换任何字符。

然后我使用 SHIFT-JS 代码页尝试了 iconv 库:

bytes_converted = 0;
char input[4096] = {0};
char dst[4096] = {0};
char* src = input;
size_t dstlen = sizeof(dst);
size_t srclen = 0;
iconv_t conv = iconv_open("UTF-8", "SHIFT-JIS" );

// make a copy
memcpy( (void*)input, (void*)kanji_win_raw_bytes, sizeof(kanji_win_raw_bytes) );
srclen = sizeof(kanji_win_raw_bytes);

if ( conv != (iconv_t)-1 )
{
    bytes_converted = iconv( conv, NULL, NULL, (char**)&dst, &dstlen );
    if ( bytes_converted == (size_t) -1 )
    {
        std::cerr << "ERROR: initializing output buffer: (" << errno << ") " << strerror(errno) << std::endl;
    }
    bytes_converted = iconv(conv, (char**)&src, &srclen, (char**)&dst, &dstlen);
    if ( bytes_converted == (size_t) - 1)
    {
        std::cerr << "ERROR in conversion: (" << errno << ") " << strerror(errno) << std::endl;
        if ( errno == EINVAL )
        {
                std::cerr << "RESULT: iconv() converted " << bytes_converted << " bytes: [" << dst << "]" << std::endl;
        }

    }
    else
    {
        std::cerr << "SUCCESS: iconv() converted " << bytes_converted << " bytes: [" << dst << "]" << std::endl;
    }
    iconv_close(conv);
}
else
{
    std::cerr << "ERROR: iconv_open() failed: " << strerror(errno) << std::endl;
}

Iconv segfaults (coredumps) 使用给定的(日语)字符串。只使用了几次 iconv,我相信代码片段(从在线示例复制)是正确的,并且似乎可以使用类似的设置但不同(即:德语/法语)来自 Windows 服务器的 mbcs 字符串与基于拉丁语的语言一起使用.

即使使用 -std=c++11 编译,codecvt 函数 std::wstring_convert 似乎还没有在 linux 中实现,所以这似乎不是一个选项。

提前感谢您提供的任何帮助。

- 编辑 -

在“myk”的帮助下,我创建了一个示例应用程序,可以更好地显示我的问题。根据他的建议,我能够绕过分段错误,但是无论我选择何种语言环境,Windows MBCS 字符串都无法转换。

/**
 * MBCS test
 */

    #include <stdlib.h>
    #include <unistd.h>
    #include <stdint.h>
    #include <stdio.h>
    #include <sys/types.h>
    #include <string.h>
    #include <errno.h>

    #include <clocale>
    #include <string>
    #include <iostream>


    // 私のクラスへようこそ   (welcome to my class)
    const char* kanji_string = "私のクラスへようこそ";
    // This is what raw UTF-8 should look like
    uint8_t kanji_utf8_raw_bytes[] = { 0xE7, 0xA7, 0x81, 0xE3, 0x81, 0xAE, 0xE3, 0x82, 0xAF, 0xE3, 0x83, 0xA9, 0xE3, 0x82, 0xB9, 0xE3, 0x81, 0xB8, 0xE3, 0x82, 0x88, 0xE3, 0x81, 0x86, 0xE3, 0x81, 0x93, 0xE3, 0x81, 0x9D };

    // This is Windows MBCS using the SHIFT-JS code page
    uint8_t kanji_win_raw_bytes[] = { 0x8E, 0x84, 0x82, 0xCC, 0x83, 0x4E, 0x83, 0x89, 0x83, 0x58, 0x82, 0xD6, 0x82, 0xE6, 0x82, 0xA4, 0x82, 0xB1, 0x82, 0xBB, 0x00, 0x00, 0x00 };

    int main( int argc, char **argv )
    {
        std::setlocale(LC_ALL, "en_US.utf8");

        std::cout << "KANJI    String  [" << kanji_string << "]" << std::endl;  
        std::cout << "KANJI UTF-8 Raw  [" << kanji_utf8_raw_bytes << "]" << std::endl;  

        const char *data = (char*)kanji_win_raw_bytes;
        std::mbstate_t state = std::mbstate_t();
        size_t result = 0;

        wchar_t* buffer = (wchar_t*)malloc( sizeof(wchar_t) * (strlen((char*)data) + 1) );

        if ( buffer )
        {
            result = std::mbsrtowcs(buffer, &data, strlen(data), &state);
            if ( result == (size_t)-1 )
            {
                std::cout << "ERROR! mbsrtowcs() " << strerror(errno) << std::endl;
                std::cout << "Error at: " <<  (int32_t)( (char*)data - (char*)kanji_win_raw_bytes ) << std::endl;
            }
            else
            {
                std::wcout << "Wide string: [" << buffer << "] " << std::endl;
            }
            free( buffer );
        }

        return 0;
    }

注意:这可以使用以下命令在 Linux/Mac 上编译和运行:

g++ mbcs_test.cpp -o mbcs_test && ./mbcs_test
4

1 回答 1

2

For mbsrtowcs(), a couple of things:

1) The call:

size_t bufflen = std::mbsrtowcs(NULL, &ptr, 0, &state);

should be something like:

size_t bufflen = std::mbsrtowcs(buffer, &ptr, strlen(m_data), &state);

assuming that you have declared 'buffer' with something like:

wchar_t* buffer = (wchar_t*) malloc(sizeof(wchar_t) * (strlen(m_data) + 1));

The third parameter in mbsrtowcs(), which you set to zero, is the length of the result buffer, which is presumably why 0 characters are getting converted.

2) My experience is you need to have used setlocale() for mbsrtowcs() to work. I can't see from the code snippet, but suggest you include something like:

#include <clocale>

:

std::setlocale(LC_ALL, "en_US.utf8");
于 2014-07-17T13:03:21.543 回答