本文介绍了从unsigned char *转换为const wchar_t *的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用下面的代码将一个字符串从 unsigned char * 转换为 const wchar_t * 。我得到的错误是只有几个字被正确转换,而其余的是乱码值。

I am using the following code to convert a string from unsigned char* to 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慣敬慆瑣牯㨢㠠嵽筛猢慣敬慆瑣牯㨢㤠嵽筛猢慣敬慆瑣牯㨢ㄠ細ﵝ﷽꯽ꮫꮫꮫﺫﻮ"


推荐答案

如果您要动态分配(使用新的),您不需要硬编码缓冲区大小。

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);

这篇关于从unsigned char *转换为const wchar_t *的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 21:59