从char *到wchar *的转换无法正常工作 [英] Conversion from char * to wchar* does not work properly

查看:187
本文介绍了从char *到wchar *的转换无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到一个字符串,如:aña!aaa?a从服务器,所以我解码它,然后我传递给一个函数。



我需要处理的消息是类似于根据字母加载路径。



我的函数的头是: void SetInfo(int num,char * descr [4])接收一个数字和4个字符(句子)的数组。为了使它更容易,让我们说只需要使用descr [0]。



当我调试并到达SetInfo(),我得到确切的消息在debugg视图中:aña!aaa?a所以直到这里一切正常。
最初,我在该函数上接收的信息是一个std :: wstring,所以我的代码与该消息一起使用wstrings和字符串,但现在我收到的是一个字符,如标题所示。消息到达直到这里确定,但如果我想使用它,那么我不能,因为如果我调试并看到Descr [0]的每个位置,然后我得到

  descr [0] [0] ='a'; // ok 
descr [0] [1] ='Ã' // BAD

所以我试图将char *转换为wchar *,代码如下:

  size_t size = strlen(descr [0])+ 1; 
wchar_t * wa = new wchar_t [size];
mbstowcs(wa,descr [0],size);但是然后调试器显示 wa


$ b < has:



wa wchar_t * 0x185d4be8 La-\\\￿ffff刯2e2e牵6365⽳6f73歯6f4c楲6553䈯736f猎6e6f档6946琯7361灭6569湰2e6f琀0067\021ᡰ9740슃b8\020\210 = r



我猜这是不正确的(我想,我必须看到相同的初始消息aña!aa?a?。如果此消息很好,知道如何获得我需要的...)



所以我的问题是:我怎么能得到descr [0] [0] ='a'和descr [ 0] [1] ='ñ'我不能通过char到wchar(你已经看到我得到了)。我做错了吗?还是有其他方法?我真的很困惑,所以任何想法将非常apreciated。



之前,当我使用wstrings(和它工作如此精细),我做的像这样:

  if(word [i] == L'\x00D1'or word [i] == L'\ x00F1')//ñ或Ñ
path =PathOfÑ;

其中word [i]与descr [0] [1] wstrings。所以我知道这个词[i]是字母'ñ'。也许这有助于理解我在做什么



(btw ...我正在eclipse,在linux上工作。)

解决方案

mbstowcs 函数在C风格字符串上工作,C风格字符串的一个原因是它们有一个特殊的终止符,' \0'。您似乎没有在字符串中添加此终止符,导致 mbstowcs 超出实际字符串的范围,并给您未定义的行为


I'm getting a string like: "aña!a¡a¿a?a" from the server so I decode it and then I pass it to a function.

What I need to do with the message is something like loading paths depending the letters.

The header of my function is: void SetInfo(int num, char *descr[4]) so it receives one number and an array of 4 chars (sentences). To make it easier, let's say I just need to work only with descr[0].

When I debug and arrive there to SetInfo(), I get the exact message in the debugg view: "aña!a¡a¿a?a" so until here is all ok. Initially, the info I was receiving on that function, was a std::wstring so all my code working with that message was with wstrings and strings but now what I receive is a char as shown in the header. The message arrived until here ok, but if I want to work with it, then I can't because if I debug and see each position of Descr[0] then I get

 descr[0][0] = 'a'; //ok
 descr[0][1] = 'Ã '; // BAD

so I tried converting char* to wchar* with a code found here:

 size_t size = strlen(descr[0]) + 1;
 wchar_t* wa = new wchar_t[size];
 mbstowcs(wa,descr[0],size);

But then the debugger shows me that wa has:

wa wchar_t * 0x185d4be8 L"a-\uffffffff刯2e2e牵6365⽳6f73歯6f4c楲6553䈯736f獵6e6f档6946琯7361灭6569湰2e6f琀0067\021ᡰ9740슃b8\020\210=r"

which I suppose that is incorrect (I'm supossing that I have to see the same initial message of "aña!a¡a¿a?a". If this message is fine then I don't know how to get what I need...)

So my question is: how can I get that descr[0][0] = 'a' and descr[0][1] = 'ñ' ?? I can't pass char to wchar (you've already see what I got). Am I doing it wrong? Or is there any other way? I am really stuck on that so any idea will be very apreciated.

Before, when I was working with wstrings (and it worked so fine) I was doing something like this:

if (word[i]==L'\x00D1' or word[i]==L'\x00F1')  // ñ or Ñ
   path ="PathOfÑ";  

where word[i] is the same as descr[0][1] in that case but with wstrings. So with that i knew that this word[i] was the letter 'ñ'. Maybe this helps to understand what I'm doing

(btw...I'm working on eclipse, on linux. )

解决方案

The mbstowcs function work on C-style strings, and one of the things about C-style strings is that they have a special terminating character, '\0'. You don't seem to be adding this terminator to the string, leading mbstowcs to go out of bounds of the actual string and giving you undefined behavior.

这篇关于从char *到wchar *的转换无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆