'wcstombs producing unexpected output
I am trying to convert a wchar_t* to char* using wcstombs on Linux(CentOS7 to be specific). The current system encoding is UTF-8 and the return value of wcstombs confirms that all the characters are converted. However, when I print the resulting char* string, I see unexpected values. I am pasting the sample code below. TRACE1 is a custom macro to print values.
int length;
int size = wcstombs(NULL,wideString,0);
TRACE1("number of bytes needed for conversion :-------> %d" , size);
char str[size+1];
length = wcstombs(str, wideString, size+1);
TRACE1("characters converted :-------> %d" , length);
if(length == size)
{
str[size] = '\0';
}
After the conversion I was expecting the char* string to have characters in ascii character set as the wideString has only ascii characters. However, I see non ascii characters in the converted char* string. wideString is something that I receive as return value from .net code and I am receiving it as a 'wchar_t' to keep it uniform across Linux and windows.I am not sure if that can have an impact here?
I have tried setting the locale to the default locale and it didn't help me. Any help is greatly appreciated. I am pasting sample wide string and output string below for reference.
Below is how the original wide character string looks like
eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6
And this is how the converted string looks like
ü°<92>§¥¥ý©<90><95>¡¥ý<8b><92>¦¥<8f>ý©<94><93><85><96>ý¨<92>¤<8d><8c>ý©<98>´<9d>¢ý<93><92>¦¥<8f>ü±<92><97>©<95>ý³<92><96>¥<8e>ü±<99>¶¹<89>ü¶<92><94><8d>¤ý<94><9c><86>µ<89>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
