Пример: перевести строку из UTF8 в Unicode, затем из Unicode в 1251
если опустить проверки ошибок, то использование такое:
char * cp1251_string = unicode_to_1251(utf8_to_unicode(utf8_string));
Как реализовать unicode_to_1251 и utf8_to_unicode см. пример.
#include <windows.h>
#include <stdio.h>
wchar_t * utf8_to_unicode(char *utf8_string)
{
int err;
wchar_t * res;
int res_len = MultiByteToWideChar(
CP_UTF8, // Code page
0, // No flags
utf8_string, // Multibyte characters string
-1, // The string is NULL terminated
NULL, // No buffer yet, allocate it later
0 // No buffer
);
if (res_len == 0)
{
printf("Failed to obtain utf8 string length\n");
return NULL;
}
res = calloc(sizeof(wchar_t), res_len);
if (res == NULL)
{
printf("Failed to allocate unicode string\n");
return NULL;
}
err = MultiByteToWideChar(
CP_UTF8, // Code page
0, // No flags
utf8_string, // Multibyte characters string
-1, // The string is NULL terminated
res, // Output buffer
res_len // buffer size
);
if (err == 0)
{
printf("Failed to convert to unicode\n");
free(res);
return NULL;
}
return res;
}
char * unicode_to_1251(wchar_t *unicode_string)
{
int err;
char * res;
int res_len = WideCharToMultiByte(
1251, // Code page
0, // Default replacement of illegal chars
unicode_string, // Multibyte characters string
-1, // Number of unicode chars is not known
NULL, // No buffer yet, allocate it later
0, // No buffer
NULL, // Use system default
NULL // We are not interested whether the default char was used
);
if (res_len == 0)
{
printf("Failed to obtain required cp1251 string length\n");
return NULL;
}
res = calloc(sizeof(char), res_len);
if (res == NULL)
{
printf("Failed to allocate cp1251 string\n");
return NULL;
}
err = WideCharToMultiByte(
1251, // Code page
0, // Default replacement of illegal chars
unicode_string, // Multibyte characters string
-1, // Number of unicode chars is not known
res, // Output buffer
res_len, // buffer size
NULL, // Use system default
NULL // We are not interested whether the default char was used
);
if (err == 0)
{
printf("Failed to convert from unicode\n");
free(res);
return NULL;
}
return res;
}
int main(int argc, char ** argv)
{
char utf8_string[] = "UTF-8 + русский текст";
wchar_t * unicode_string;
char * cp1251_string;
unicode_string = utf8_to_unicode(utf8_string);
if (unicode_string == NULL)
{
printf("Failed to convert!\n" );
return 1;
}
MessageBoxW(NULL, unicode_string, L"Unicode", 0);
cp1251_string = unicode_to_1251(unicode_string);
free(unicode_string);
if (cp1251_string == NULL)
{
printf("Failed to convert from unicode!\n");
return 2;
}
MessageBoxA(NULL, cp1251_string, "CP1251", 0);
return 0;
}