Skip to content

Instantly share code, notes, and snippets.

@iaoedsz2008
Last active September 6, 2019 09:32
Show Gist options
  • Save iaoedsz2008/bd1b748a4b2d9fa835b1246c82f49b6d to your computer and use it in GitHub Desktop.
Save iaoedsz2008/bd1b748a4b2d9fa835b1246c82f49b6d to your computer and use it in GitHub Desktop.
convert utf8 to localcharset
#include <iconv.h>
#include <localcharset.h>
static iconv_t g_utf8_to_localcharset;
static int init_iconv ()
{
g_utf8_to_localcharset = NULL;
#if _WIN32
const char* charset = locale_charset ();
if (strcmp (charset, "UTF-8") != 0)
{
g_utf8_to_localcharset = iconv_open (charset, "UTF-8");
if (g_utf8_to_localcharset == (void*)-1)
{
g_utf8_to_localcharset = NULL;
return -1;
}
}
#endif
return 0;
}
static char* to_utf8 (const char* str)
{
char* p_str = NULL;
if (g_utf8_to_localcharset)
{
size_t src_len = strlen (str);
size_t dst_len = src_len * 4;
char* src_str = str;
char* dst_str = (char*)calloc (1, dst_len);
p_str = dst_str;
iconv (g_utf8_to_localcharset, &src_str, &src_len, &dst_str, &dst_len);
}
else
{
p_str = strdup (str);
}
return p_str;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment