简体   繁体   English

从无符号char *到const wchar_t *的转换

[英]conversion from unsigned char* to const wchar_t*

I am using the following code to convert a string from unsigned char* to const wchar_t* . 我正在使用以下代码将字符串从unsigned char*转换为const wchar_t* The error I am getting is that only a few words are being converted properly while the rest is garbled value. 我得到的错误是只有少数几个单词被正确转换,而其余的则是乱码。

CODE

unsigned char* temp = fileUtils->getFileData("levels.json", "r", &size);
const char* temp1 = reinterpret_cast<const char*>(temp);
size_t len = mbstowcs(nullptr, &temp1[0], 0);
if (len == -1) {

} else {
    wchar_t* levelData = new wchar_t();
    mbstowcs(&levelData[0], &temp1[0], size*10);
}

OUTPUT 输出值

temp1 = "[{"scaleFactor": 1}][{"scaleFactor": 2}][{"scaleFactor": 3}][{"scaleFactor": 4}][{"scaleFactor": 5}][{"scaleFactor": 6}][{"scaleFactor": 7}][{"scaleFactor": 8}][{"scaleFactor": 9}][{"scaleFactor": 10}]"

levelData = "[{"scaleFactor": 1}][{"scaleFactor": 2}][{"scaleFactor": 3}][{"scaleFactor": 4}][{"scaleFactor": 5}][{"scaleFactor": 6}][{"scaleFactor": 7}][{"s慣敬慆瑣牯㨢㠠嵽筛猢慣敬慆瑣牯㨢㤠嵽筛猢慣敬慆瑣牯㨢ㄠ細ﵝ﷽꯽ꮫꮫꮫﺫﻮ"
wchar_t* levelData = new wchar_t();
mbstowcs(&levelData[0], &temp1[0], size*10);

That allocated enough memory for exactly ONE character. 这样就为一个字符分配了足够的内存。 That's not enough to store your string, so of course things will not work right. 这还不足以存储您的字符串,因此当然无法正常工作。

Also, where'd that 10 come from? 还有,那10从哪里来?

You don't need to hard code the buffer size if you're going to allocate it dynamically (with new). 如果要动态分配(使用新的)缓冲区大小,则无需硬编码缓冲区大小。

wchar_t* levelData = new wchar_t[len+1];
mbstowcs(&levelData[0], &temp1[0], len);

Thanks to @BenVoigt, found the mistake. 感谢@BenVoigt,发现了错误。 Changed the code to this- 将代码更改为此

wchar_t levelData[200];
mbstowcs(&levelData[0], &temp1[0], size);
unsigned char* temp = fileUtils->getFileData("levels.json", "r", &size);
const char* temp1 = reinterpret_cast<const char*>(temp);

wchar_t* levelData = new wchar_t[size];
int last_char_size = 0;

mbtowc(NULL, 0, 0);
for (wchar_t* position = levelData; size > 0; position++)
{
    last_char_size = mbtowc(position, temp1, size);
    if (last_char_size <= 0) break;
    else {
        temp1 += last_char_size;
        size -= last_char_size;
    }
}

if (last_char_size == -1)
{
    std::cout  << "Invalid encoding" << std::endl;
}

delete[] temp; // * probably

The marked line (*) depends on, whether the fileUtils->getFileData allocates a memory block for temp and the object of fileUtils does not manage it by its own. 标记的行(*)取决于fileUtils->getFileData是否为temp分配了一个内存块,而fileUtils的对象不自行管理它。 -- Which is most probable. -这是最有可能的。 However you should check the documentation. 但是,您应该检查文档。

The size should be perfectly enough size for the levelData array, while whithin [] you specify the number of elements of the array, not the number of bytes(aka char s). size应完全适合levelData数组,而[]指定数组元素的数量,而不是字节数(即char )。 - In this case, it is the number of wide characters. -在这种情况下,它是宽字符的数量。 Which can not be more, then read char s. 哪个不能更多,然后阅读char s。

Another thing you should be aware, the fileUtils->getFileData probably reads binary date. 您应该知道的另一件事, fileUtils->getFileData可能读取二进制日期。 So the text in temp is not followed by 0. So, later string functions - like wcstok - called on it, will shoot your foot off. 因此, temp的文本后不跟0。因此,稍后调用的字符串函数(如wcstok将使您大wcstok

And one another. 和另一个。 If you are not familiar with the construction 如果您不熟悉构造

    function_on_arrays( target,  source,  size )

Remember your program in C/C++ don't know the sizes of target and source . 请记住,您使用C / C ++编写的程序不知道targetsource的大小。 But probably, you do not want to the function do something beyond them. 但是可能您不希望函数执行超出其范围的操作。 So this is what for the size mainly is. 所以这主要是size - Your manual way to say, on how many elements you want to perform the action to not go beyond the arrays's data. -您要手动执行的操作,即要执行多少个元素而不超出数组的数据。

Edit: The earlier solution was wrong, as mistakenly treating the last parameter of mbstowcs as the number of characters in the source. 编辑:较早的解决方案是错误的,因为错误地将mbstowcs的最后一个参数视为源中的字符数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM