MBCS character code support
Katayama Hirofumi MZ
katayama.hirofumi.mz at gmail.com
Tue May 15 20:39:45 PDT 2012
You can convert UTF-8 to Shift_JIS by the following code.
/* Linux, FreeBSD or UNIX */
#include <iconv.h>
iconv_t g_icUTF8toSJIS;
char *convert_utf8_to_sjis(char *in)
{
char *out, *p_in, *p_out,
size_t in_size, out_size;
in_size = strlen(in);
out_size = in_size;
out = (char *)malloc(out_size + 1);
if (out == NULL)
return NULL;
p_in = in;
p_out = out;
iconv(g_icUTF8toSJIS, &p_in, &in_size, &p_out, &out_size);
*p_out = 0;
return out;
}
int main(void)
{
char *out;
g_icUTF8toSJIS = iconv_open("UTF-8", "SJIS");
if (g_icUTF8toSJIS == (iconv_t)-1) {
// error
}
...
out = convert_utf8_to_sjis(...);
...
free(out);
...
iconv_close(g_icUTF8toSJIS);
return 0;
}
/* Windows */
#include <windows.h>
char *UTF8toSJIS(char *utf8)
{
char *wide, *sjis;
int size;
size = MultiByteToWideChar(CP_UTF8, 0, utf8, -1, 0, 0);
wide = (char *)malloc((size + 1) * sizeof(WCHAR));
if (wide == NULL)
return NULL;
MultiByteToWideChar(CP_UTF8, 0, utf8, -1, wide, size);
size = WideCharToMultiByte(CP_ACP, 0, wide, -1, 0, 0, 0, 0);
sjis = malloc(size * 2 + 1);
if (sjis == NULL) {
free(wide);
return NULL;
}
WideCharToMultiByte(CP_ACP, 0, wide, -1, sjis, size, 0, 0);
free(wide);
return sjis;
}
int main(void)
{
char *out;
...
out = UTF8toSJIS(...);
...
free(out);
...
return 0;
}
More information about the Digitalmars-d
mailing list