* Wide character conversion
@ 2006-06-08 13:29 romyd misc
2006-06-08 14:13 ` John Love-Jensen
0 siblings, 1 reply; 2+ messages in thread
From: romyd misc @ 2006-06-08 13:29 UTC (permalink / raw)
To: gcc-help
Hi All,
I'm new to gcc and i was trying to write a code to convert a char * to
wchar * and i found an example on
http://www.gnu.org/software/libc/manual/html_node/Converting-a-Character.html
Following is the code snippet
wchar_t * mbstouwcs (const char *s)
{
size_t len = strlen (s);
// wchar_t *result = malloc ((len + 1) * sizeof (wchar_t));
wchar_t *result = (wchar_t *)calloc(16 * 2,'\0');
wchar_t *wcp = result;
wchar_t tmp;
mbstate_t state;
size_t nbytes;
int i = 0;
// printf("Len%d\n",len);
memset (&state, '\0', sizeof (state));
while ((nbytes = mbrtowc (&tmp, s, len, &state)) > 0)
{
if (nbytes >= (size_t) -2)
/* Invalid input string. */
return NULL;
// printf("Counter%d\n",i);
wprintf(L"Tmp%c",tmp);
// *result++ = tmp;
// printf("Counter%d\n",i);
result[i] = tmp;
// *result++;
i++;
len -= nbytes;
s += nbytes;
}
result[i] = '\0';
printf("Test\n");
wprintf(L"Size%d\n",sizeof(result));
wprintf(L"Result%s\n",result);
return result;
}
When i try to print result, i only get the 1st character. Can any one
help me fixing this code, so it returns all the characters converted
to wchar in result. Please do send suggestions if anyone knows a
different way of converting a char * to wchar*.
Thanks,
Romy
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: Wide character conversion
2006-06-08 13:29 Wide character conversion romyd misc
@ 2006-06-08 14:13 ` John Love-Jensen
0 siblings, 0 replies; 2+ messages in thread
From: John Love-Jensen @ 2006-06-08 14:13 UTC (permalink / raw)
To: romyd misc, MSX to GCC
Hi Romy,
You are using "%c" to printf a wchar_t. Is that intentional? Shouldn't you
use a "%lc"?
It appears that the routine relies on sizeof(wchar_t) == 2. Does your
sizeof(wchar_t) == 2 on your platform?
You are using "%s" to printf a wchar_t*. Is that intentional? Shouldn't
you use a "%ls"?
HTH,
--Eljay
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2006-06-08 14:13 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-06-08 13:29 Wide character conversion romyd misc
2006-06-08 14:13 ` John Love-Jensen
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).