Unicode conversion

D

djake

How can I convert char in wchar_t?
And how can I convert wchar_t in char?

Thanks to anyone
 
M

Mike Wahler

How can I convert char in wchar_t?

wchar_t w = 0;
char c = 'A';

w = c;

However, this has nothing to do with Unicode
(which is not topical here anyway).
And how can I convert wchar_t in char?

c = (char)w;

... however, data may be lost, as type 'char'
might not have the same range as 'wchar_t'.

IMO you'll need to think more about exactly what
problem you're trying to solve, and forumlate
a much more specific question(s). Then if it's
about the C *language*, ask here. If it's not,
find a newsgroup where it's topical.

Note that Unicode questions are not C questions.

-Mike
 
K

Keith Thompson

Mike Wahler said:
wchar_t w = 0;
char c = 'A';

w = c;

However, this has nothing to do with Unicode
(which is not topical here anyway).


c = (char)w;

.. however, data may be lost, as type 'char'
might not have the same range as 'wchar_t'.
[...]

The cast is superfluous. char and wchar_t are both integer types, so
an assignment includes an implicit conversion.
 
M

Mike Wahler

Keith Thompson said:
Mike Wahler said:
wchar_t w = 0;
char c = 'A';

w = c;

However, this has nothing to do with Unicode
(which is not topical here anyway).


c = (char)w;

.. however, data may be lost, as type 'char'
might not have the same range as 'wchar_t'.
[...]

The cast is superfluous. char and wchar_t are both integer types, so
an assignment includes an implicit conversion.

That was an attempt to pre-empt a possible compiler warning. :)

-Mike
 
S

Simon Biber

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;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,170
Messages
2,570,925
Members
47,464
Latest member
Bobbylenly

Latest Threads

Top