dj***@excite.it wrote:
How can I convert char in wchar_t?
And how can I convert wchar_t in char?
Thanks to anyone
Basically, set the LC_CTYPE of the locale to tell the implementation
what encoding your char string is in, then use wcstombs and mbstowcs
functions to perform the conversion.
The following code demonstrates their use.
(Can an expert here please check my code? It seems to work fine, but
there may be off-by-one errors in the memory allocation and/or error
checking.)
#include <locale.h>
#include <stdio.h>
#include <stdlib.h>
char * /// convert wchar_t string to char in given encoding
wchar_t_string_ to_char(const wchar_t *src,
const char *encoding)
{
if(setlocale(LC _CTYPE, encoding) == NULL)
{
fprintf(stderr, "requested encoding unavailable\n") ;
return NULL;
}
size_t n = wcstombs(NULL, src, 0);
char *dst = malloc(n + 1);
if(dst == NULL)
{
fprintf(stderr, "memory allocation failed\n");
return NULL;
}
if(wcstombs(dst , src, n + 1) != n)
{
fprintf(stderr, "conversion failed\n");
free(dst);
return NULL;
}
return dst;
}
wchar_t * /// convert char string in given encoding to wchar_t
char_string_to_ wchar_t(const char *src,
const char *encoding)
{
if(setlocale(LC _CTYPE, encoding) == NULL)
{
fprintf(stderr, "requested encoding unavailable\n") ;
return NULL;
}
size_t n = mbstowcs(NULL, src, 0);
wchar_t *dst = malloc((n + 1) * sizeof *dst);
if(!dst)
{
fprintf(stderr, "memory allocation failed\n");
return NULL;
}
if(mbstowcs(dst , src, n + 1) != n)
{
fprintf(stderr, "conversion failed\n");
free(dst);
return NULL;
}
return dst;
}
int /// test the above functions
main(void)
{
char utf8[] = {0xE4, 0xBD, 0xA0, 0xE5, 0xA5, 0xBD, 0};
wchar_t unicode[] = {0x4F60, 0x597D, 0};
const char *encoding = "en_US.UTF-8"; // or try en_US.ISO-8859-1 etc.
char *converted1 = wchar_t_string_ to_char(unicode , encoding);
wchar_t *converted2 = char_string_to_ wchar_t(utf8, encoding);
if(converted1)
{
printf("Unicode converted to UTF8: ");
for(char *p = converted1; *p; p++)
printf("%X ", (unsigned)(unsi gned char)*p);
free(converted1 );
}
if(converted2)
{
printf("\nUTF8 converted to Unicode: ");
for(wchar_t *p = converted2; *p; p++)
printf("%X ", (unsigned)*p);
free(converted2 );
}
putchar('\n');
return 0;
}