从unsigned char *转换为const wchar

编程入门 行业动态 更新时间:2024-10-23 14:22:11
本文介绍了从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

本文发布于:2023-10-07 14:30:32,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1469636.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:转换为   char   unsigned   const   wchar

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!