ascii编码与utf8互转、ascii编码与unicode互转、utf8与unicode互转(C语言、c++)

文章详细描述了在Windows和Linux环境下,如何使用ASCII和UTF-8编码进行转换,提供了相应的API函数实现,包括ASCII转UTF-8、UTF-8转ASCII,以及Unicode之间的转换。
摘要由CSDN通过智能技术生成

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

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值