
ascii编码与utf8互转、ascii编码与unicode互转、utf8与unicode互转(C语言、c++)
【代码】ascii编码与utf8互转、ascii编码与unicode互转、utf8与unicode互转(C语言、c++)
·
ascii 转 uft8:
int ascii_to_utf8(const char * asciibuf, int asciilen, char * utf8buf, int utf8maxlen)
{
int convresult = 0;
#ifdef OS_WIN
DWORD dwRlt = ERROR_SUCCESS;
WCHAR * newBuf = NULL;
try
{
int newbuf_maxlen = asciilen * 2 + 1;
newBuf = new WCHAR[newbuf_maxlen];
CheckNewRlt(newBuf, newbuf_maxlen * sizeof(WCHAR));
int unicode_len = MultiByteToWideChar (CP_ACP, 0,
asciibuf, asciilen,
newBuf,newbuf_maxlen);
convresult = ::WideCharToMultiByte(CP_UTF8, 0,
newBuf,unicode_len,
utf8buf, utf8maxlen, NULL, NULL);
}
catch (DWORD dwExp)
{
dwRlt = dwExp;
}
catch (...)
{
dwRlt = ERROR_EXCEPTION_IN_SERVICE;
}
SafeDeleteAry(newBuf);
#endif // OS_WIN
#ifdef OS_LINUX
char * cvt_src_buf = (char *)asciibuf;
size_t cvt_src_len = asciilen;
char * cvt_dst_buf = (char *)utf8buf;
size_t cvt_dst_len = utf8maxlen;
convresult = CUnicodeWithAscii::common_conv(enum_coding_cvt_at8,
(char **)&(cvt_src_buf), &cvt_src_len,
(char **)&(cvt_dst_buf), &cvt_dst_len);
#endif // OS_LINUX
return convresult;
}
utf8转ascii:
int utf8_to_ascii(const char * utf8buf, int utf8len,
char * asciibuf, int asciimaxlen)
{
int convresult = 0;
#ifdef OS_WIN
// windows下转换必须先转换为unicode,然后转utf-8
DWORD dwRlt = ERROR_SUCCESS;
WCHAR * newBuf = NULL;
try
{
int newbuf_maxlen = utf8len * 2 + 1;
newBuf = new WCHAR[newbuf_maxlen];
CheckNewRlt(newBuf, newbuf_maxlen * sizeof(WCHAR));
int unicode_len = MultiByteToWideChar (CP_UTF8, 0,
utf8buf, utf8len,
newBuf,newbuf_maxlen);
convresult = ::WideCharToMultiByte(CP_ACP, 0,
newBuf,unicode_len,
asciibuf, asciimaxlen, NULL, NULL);
}
catch (DWORD dwExp)
{
dwRlt = dwExp;
}
catch (...)
{
dwRlt = ERROR_EXCEPTION_IN_SERVICE;
}
SafeDeleteAry(newBuf);
#endif
#ifdef OS_LINUX
char * cvt_src_buf = (char *)utf8buf;
size_t cvt_src_len = utf8len;
char * cvt_dst_buf = (char *)asciibuf;
size_t cvt_dst_len = asciimaxlen;
convresult = CUnicodeWithAscii::common_conv(enum_coding_cvt_8ta,
(char **)&(cvt_src_buf), &cvt_src_len,
(char **)&(cvt_dst_buf), &cvt_dst_len);
#endif
return convresult ;
}
acsii转unicode:
int ascii_to_unicode(const char * asciibuf, int asciilen,
WCHAR * unibuf, int unibuflen)
{
int convresult = 0;
#ifdef OS_WIN
convresult = MultiByteToWideChar(CP_ACP,
0,
asciibuf,
asciilen,
unibuf,
unibuflen);
#endif
#ifdef OS_LINUX
char * cvt_src_buf = (char *)asciibuf;
size_t cvt_src_len = asciilen;
char * cvt_dst_buf = (char *)unibuf;
size_t cvt_dst_len = unibuflen * sizeof(WCHAR);
convresult = CUnicodeWithAscii::common_conv(enum_coding_cvt_atu,
(char **)&(cvt_src_buf), &cvt_src_len,
(char **)&(cvt_dst_buf), &cvt_dst_len);
convresult = convresult / sizeof(WCHAR);
#endif
return convresult ;
}
unicode转ascii:
int unicode_to_ascii(const WCHAR * unibuf, int unibuflen,
char * asciibuf, int asciimaxlen)
{
int convresult = 0;
#ifdef OS_WIN
convresult = ::WideCharToMultiByte(CP_ACP, 0, unibuf,
unibuflen,asciibuf, asciimaxlen, NULL, NULL);
#endif
#ifdef OS_LINUX
char * cvt_src_buf = (char *)unibuf;
size_t cvt_src_len = unibuflen * sizeof(WCHAR);
char * cvt_dst_buf = (char *)asciibuf;
size_t cvt_dst_len = asciimaxlen;
convresult = CUnicodeWithAscii::common_conv(enum_coding_cvt_uta,
(char **)&(cvt_src_buf), &cvt_src_len,
(char **)&(cvt_dst_buf), &cvt_dst_len);
#endif
if (convresult < asciimaxlen)
{
asciibuf[convresult] = '\0';
}
return convresult;
}
utf8转unicode:
int utf8_to_unicode(const char * utf8buf, int utf8len,
WCHAR * unibuf, int unibuflen)
{
int convresult = 0;
#ifdef OS_WIN
convresult = MultiByteToWideChar(CP_UTF8,
0,
utf8buf,
utf8len,
unibuf,
unibuflen);
#endif
#ifdef OS_LINUX
char * cvt_src_buf = (char *)utf8buf;
size_t cvt_src_len = utf8len;
char * cvt_dst_buf = (char *)unibuf;
size_t cvt_dst_len = unibuflen * sizeof(WCHAR);
convresult = CUnicodeWithAscii::common_conv(enum_coding_cvt_8tu,
(char **)&(cvt_src_buf), &cvt_src_len,
(char **)&(cvt_dst_buf), &cvt_dst_len);
convresult = convresult / sizeof(WCHAR);
#endif
return convresult ;
}
unicode转utf8:
int unicode_to_utf8(const WCHAR * unibuf, int unibuflen, char * utf8buf, int utf8maxlen)
{
int convresult = 0;
#ifdef OS_WIN
convresult = ::WideCharToMultiByte(CP_UTF8, 0, unibuf,
unibuflen,utf8buf, utf8maxlen, NULL, NULL);
#endif
#ifdef OS_LINUX
char * cvt_src_buf = (char *)unibuf;
size_t cvt_src_len = unibuflen * sizeof(WCHAR);
char * cvt_dst_buf = (char *)utf8buf;
size_t cvt_dst_len = utf8maxlen;
convresult = CUnicodeWithAscii::common_conv(enum_coding_cvt_ut8,
(char **)&(cvt_src_buf), &cvt_src_len,
(char **)&(cvt_dst_buf), &cvt_dst_len);
#endif
return convresult;
}
更多推荐
所有评论(0)