Re: Unicode conversion
- From: Simon Biber <news@xxxxxxxxx>
- Date: Wed, 19 Oct 2005 15:43:59 +1000
djake@xxxxxxxxx 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)(unsigned 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;
}
.- References:
- Unicode conversion
- From: djake
- Unicode conversion
- Prev by Date: Re: Two-Dimensional Char Array
- Next by Date: Re: thread safety
- Previous by thread: Re: Unicode conversion
- Next by thread: Help. What is the error?
- Index(es):
Relevant Pages
|