编码转换问题

2 篇文章 0 订阅
2 篇文章 0 订阅
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iconv.h>
/**
 * 中文字串转为utf8格式字串
 * src  中文字串
 * ret  utf8字串
 * len  src串的长度
 * 返回转换后的utf8串的长度
 * 说明:ret大小应该是src的1.5倍以上。一个汉字转为了3个字节的utf编码
 **/
int To_utf8(unsigned char *src,unsigned char *ret,int len){
	iconv_t c_p;
	char *in,*out;
	int i,i_len,o_len;
	unsigned char tmp[1500];
	memset(tmp,0,sizeof(tmp));
	c_p=iconv_open("UTF-8","GBK"); //GBK 转为 UTF-8
	in=(char *)src;out=(char *)tmp;
	i_len=len;o_len=1500;
	iconv(c_p,&in,(size_t*)&i_len,&out,(size_t*)&o_len);
	i=strlen((char *)tmp);
	memcpy(ret,tmp,i);
	iconv_close(c_p);
	return i;
}
 
/**
 * utf8格式字串转为中文字串
 * src  utf8字串
 * ret  中文字串
 * len  src串的长度
 * 返回转换后的中文串的长度
 **/
int To_gbk(unsigned char *src,unsigned char *ret,int len){
	iconv_t c_p;
	char *in,*out;
	int i,i_len,o_len;
	unsigned char tmp[1500];
	memset(tmp,0,sizeof(tmp));
	c_p=iconv_open("GBK","UTF-8"); //UTF-8转为 GBK 
	in=(char *)src;out=(char *)tmp;
	i_len=len;o_len=1500;
	iconv(c_p,&in,(size_t*)&i_len,&out,(size_t*)&o_len);
	i=strlen((char *)tmp);
	memcpy(ret,tmp,i);
	iconv_close(c_p);
	return i;
}

//string 转utf8 windows环境
#include<windows.h>
std::string string_To_UTF8(const std::string& str)
{
	int nwLen = ::MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, NULL, 0);
	wchar_t* pwBuf = new wchar_t[nwLen + 1];//一定要加1,不然会出现尾巴 
	ZeroMemory(pwBuf, nwLen * 2 + 2);
	::MultiByteToWideChar(CP_ACP, 0, str.c_str(), str.length(), pwBuf, nwLen);
	int nLen = ::WideCharToMultiByte(CP_UTF8, 0, pwBuf, -1, NULL, NULL, NULL, NULL);
	char* pBuf = new char[nLen + 1];
	ZeroMemory(pBuf, nLen + 1);
	::WideCharToMultiByte(CP_UTF8, 0, pwBuf, nwLen, pBuf, nLen, NULL, NULL);
	std::string retStr(pBuf);
	delete[]pwBuf;
	delete[]pBuf;
	pwBuf = NULL;
	pBuf = NULL;
	return retStr;
}


char* string_to_gbk( const char* chUTF )
{
	int len = MultiByteToWideChar(CP_UTF8, 0, chUTF, -1, NULL, 0);
	unsigned short * wszGBK = new unsigned short[len + 1];
	memset(wszGBK, 0, len * 2 + 2);
	MultiByteToWideChar(CP_UTF8, 0, (LPCTSTR)chUTF, -1, (LPWSTR)wszGBK, len);
 
	len = WideCharToMultiByte(CP_ACP, 0, (LPWSTR)wszGBK, -1, NULL, 0, NULL, NULL);
	char *szGBK = new char[len + 1];
	memset(szGBK, 0, len + 1);
	WideCharToMultiByte(CP_ACP,0, (LPWSTR)wszGBK, -1, szGBK, len, NULL, NULL);
	delete[]wszGBK;
	return szGBK;
}

char* gdb_to_utf8( const char* chGBK )
{
	DWORD dWideBufSize=MultiByteToWideChar(CP_ACP, 0,(LPCSTR)chGBK,-1, NULL, 0);  
	wchar_t * pWideBuf=new wchar_t[dWideBufSize];  
	wmemset(pWideBuf, 0, dWideBufSize);  
	MultiByteToWideChar(CP_ACP,0,(LPCSTR)chGBK,-1,pWideBuf,dWideBufSize);
 
	DWORD dUTF8BufSize=WideCharToMultiByte(CP_UTF8,0,(LPCWSTR)pWideBuf,-1,NULL,0,NULL,NULL); 
 
	char * pUTF8Buf=new char[dUTF8BufSize];  
	memset(pUTF8Buf, 0, dUTF8BufSize);  
	WideCharToMultiByte( CP_UTF8,0,(LPCWSTR)pWideBuf,-1,pUTF8Buf,dUTF8BufSize,NULL,NULL);
 
	delete[]pWideBuf;
	return pUTF8Buf;
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值