Convert from unsigned char * to const wchar_t *

I use the following code to convert a string from unsigned char* to const wchar_t* . The error I get is that only a few words are converted correctly, and the rest are distorted values.

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慣敬慆瑣牯㨢㠠嵽筛猢慣敬慆瑣牯㨢㤠嵽筛猢慣敬慆瑣牯㨢ㄠ細ﵝ﷽꯽ꮫꮫꮫﺫﻮ" 
0
source share
4 answers

You do not need to hardcode the size of the buffer if you are going to dynamically allocate it (with a new one).

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

This allocated enough memory for ONLY ONE character. This is not enough to store your string, so of course, everything will not work correctly.

Also, where did that 10 come from?

+3
source

Thanks @BenVoigt, found the error. Changed the code for this -

 wchar_t levelData[200]; mbstowcs(&levelData[0], &temp1[0], size); 
0
source
 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 fileUtils->getFileData block of memory for temp , and the fileUtils object fileUtils not manage it on its own. - What is most likely. However, you should check the documentation.

size should be a sufficient size for the levelData array, while whithin [] you specify the number of elements in the array, not the number of bytes (aka char s). - In this case, this is the number of wide characters. Which couldn't be bigger then read char s.

Another thing you should know about, fileUtils->getFileData is probably reading a binary date. Thus, the text in temp not followed by 0. Thus, subsequent string functions like wcstok - calling it will take your foot off.

And with each other. If you are not familiar with the construction

  function_on_arrays( target, source, size ) 

Remember that your program in C / C ++ does not know the sizes of target and source . But you probably do not want the function to do anything beyond them. So that's what for size basically. - Your manual way to say how many elements you want to perform so as not to go beyond the data arrays.

Edit: The earlier solution was wrong, as it mistakenly treated the last mbstowcs parameter as the number of characters in the source.

0
source

All Articles