C/C++ 编码转换


(1) iconv_t iconv_open(const char *tocode, const char *fromcode);
//此函数说明将要进行哪两种编码的转换,tocode是目标编码,fromcode是原编码,该函数返回一个转换句柄,供以下两个函数使用。
(2) size_t iconv(iconv_t cd,char **inbuf,size_t *inbytesleft,char **outbuf,size_t *outbytesleft);
//此函数从inbuf中读取字符,转换后输出到outbuf中,inbytesleft用以记录还未转换的字符数,outbytesleft用以记录输出缓冲的剩余空间。
(3) int iconv_close(iconv_t cd);
//此函数用于关闭转换句柄,释放资源。

C++ Linux实现

#ifndef WIN32
#include <iconv.h>
#define OUTLEN 500
class CodeConverter {

private:
	iconv_t cd;
public:

	// 构造
	CodeConverter(const char *from_charset, const char *to_charset) {
		cd = iconv_open(to_charset, from_charset);
	}


	// 析构
	~CodeConverter() {
		iconv_close(cd);
	}


	// 转换输出
	int convert(char *inbuf, int inlen, char *outbuf, int outlen) {
		char **pin = &inbuf;
		char **pout = &outbuf;

		//memset(outbuf, 0, outlen);
		return iconv(cd, pin, (size_t *)&inlen, pout, (size_t *)&outlen);
	}
};

C Linux实现

char *in_utf8 = "姝e?ㄥ??瑁?";
char *in_gb2312 = "正在安装";
char out[OUTLEN];
 
/*unicode码转为gb2312码*/
rc = u2g(in_utf8,strlen(in_utf8),out,OUTLEN);
printf("unicode-->gb2312 out=%sn",out);
//gb2312码转为unicode码
 
rc = g2u(in_gb2312,strlen(in_gb2312),out,OUTLEN);
printf("gb2312-->unicode out=%sn",out);
}
/*代码转换:从一种编码转为另一种编码*/
int code_convert(char *from_charset,char *to_charset,char *inbuf,int inlen,char *outbuf,int outlen)
{
iconv_t cd;
int rc;
char **pin = &inbuf;
char **pout = &outbuf;
 
cd = iconv_open(to_charset,from_charset);
if (cd==0) return -1;
memset(outbuf,0,outlen);
if (iconv(cd,pin,&inlen,pout,&outlen)==-1) return -1;
iconv_close(cd);
return 0;
}
/*UNICODE码转为GB2312码*/
int u2g(char *inbuf,int inlen,char *outbuf,int outlen)
{
return code_convert("utf-8","gb2312",inbuf,inlen,outbuf,outlen);
}
/*GB2312码转为UNICODE码*/
int g2u(char *inbuf,size_t inlen,char *outbuf,size_t outlen)
{
return code_convert("gb2312","utf-8",inbuf,inlen,outbuf,outlen);
}


转换接口

string UTF8ToANSI(const string str)
	{
#ifdef WIN32
		return UnicodeToANSI(UTF8ToUnicode(str));
#else
		static char out[OUTLEN] = { 0 };
		memset(out, 0, OUTLEN);
		// utf-8-->gb2312
		char* pTmp = (char*)str.c_str();
		CodeConverter cc = CodeConverter("utf-8", "gb2312");
		cc.convert(pTmp,strlen(pTmp), out, OUTLEN);
		::string strtmp(out, strlen(out));
		return strtmp;
#endif
	}

	string ANSIToUTF8(const string str)
	{
#ifdef WIN32
		return UnicodeToUTF8(ANSIToUnicode(str));
#else
		static char out[OUTLEN];
		memset(out, 0, OUTLEN);
		CodeConverter cc2 = CodeConverter("gb2312", "utf-8");
		cc2.convert((char*)str.c_str(), str.length(), out, OUTLEN);
		::string strtmp (out, strlen(out));
		return strtmp;
#endif
	}

windows实现

ANSI TO UFT8

string UnicodeToUTF8(const wstring& str)
	{
#ifdef WIN32
		char*     pElementText;
		int    iTextLen;
		iTextLen = WideCharToMultiByte(CP_UTF8, 0, str.c_str(), -1, NULL, 0, NULL, NULL);
		pElementText = new char[iTextLen + 1];
		memset((void*)pElementText, 0, sizeof(char) * (iTextLen + 1));
		::WideCharToMultiByte(CP_UTF8, 0, str.c_str(), -1, pElementText, iTextLen, NULL, NULL);
		string strText;
		strText = pElementText;
		delete[] pElementText;
		return strText;
#else
		return "";
#endif
	}
	wstring ANSIToUnicode(const string& str)
	{
#ifdef WIN32
		int  len = 0;
		len = str.length();
		int  unicodeLen = ::MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, NULL, 0);
		wchar_t *  pUnicode;
		pUnicode = new  wchar_t[unicodeLen + 1];
		memset(pUnicode, 0, (unicodeLen + 1) * sizeof(wchar_t));
		::MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, (LPWSTR)pUnicode, unicodeLen);
		wstring  rt;
		rt = (wchar_t*)pUnicode;
		delete  pUnicode;
		return  rt;
#else
		wstring  rt;
		return rt;


UTF8 TO ANSI

wstring UTF8ToUnicode(const string& str)
	{
#ifdef WIN32
		int  len = 0;
		len = str.length();
		int  unicodeLen = ::MultiByteToWideChar(CP_UTF8,0,str.c_str(),-1,NULL,0);
		wchar_t *  pUnicode;
		pUnicode = new  wchar_t[unicodeLen + 1];
		memset(pUnicode, 0, (unicodeLen + 1) * sizeof(wchar_t));
		::MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, (LPWSTR)pUnicode, unicodeLen);
		wstring  rt;
		rt = (wchar_t*)pUnicode;
		delete  pUnicode;
		return  rt;
#else
		wstring  rt;
		return rt;
#endif	
	}

	string UnicodeToANSI(const wstring& str)
	{
#ifdef WIN32
		char*     pElementText;
		int    iTextLen;
		// wide char to multi char
		iTextLen = WideCharToMultiByte(CP_ACP,0,str.c_str(),-1,NULL,0,NULL,NULL);
		pElementText = new char[iTextLen + 1];
		memset((void*)pElementText, 0, sizeof(char) * (iTextLen + 1));
		::WideCharToMultiByte(CP_ACP, 0, str.c_str(), -1, pElementText, iTextLen, NULL, NULL);
		string strText;
		strText = pElementText;
		delete[] pElementText;
		return strText;







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在C/C++中将PEM格式的公钥转换为PublicKey对象的步骤如下: 1. 将PEM格式的公钥字符串读取到一个char数组中。 2. 去掉PEM格式字符串中的头部和尾部,只保留公钥部分。 3. 将剩余的公钥部分进行Base64解码,得到公钥的二进制数据。 4. 将公钥的二进制数据转换为PublicKey对象。 下面是一个示例代码: ```c #include <openssl/pem.h> #include <openssl/rsa.h> #include <openssl/err.h> #include <openssl/bio.h> #include <stdio.h> #include <string.h> #include <stdlib.h> RSA* get_public_key(const char *pem_public_key) { const char *header = "-----BEGIN PUBLIC KEY-----"; const char *footer = "-----END PUBLIC KEY-----"; // 去掉PEM格式字符串中的头部和尾部,只保留公钥部分。 const char *pem_public_key_start = strstr(pem_public_key, header); const char *pem_public_key_end = strstr(pem_public_key, footer); if (pem_public_key_start == NULL || pem_public_key_end == NULL) return NULL; pem_public_key_start += strlen(header); int pem_public_key_len = pem_public_key_end - pem_public_key_start; char *pem_public_key_body = (char *)malloc(pem_public_key_len + 1); memcpy(pem_public_key_body, pem_public_key_start, pem_public_key_len); pem_public_key_body[pem_public_key_len] = '\0'; // 对剩余的公钥部分进行Base64解码,得到公钥的二进制数据。 BIO *bio = BIO_new_mem_buf(pem_public_key_body, -1); RSA *rsa = PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, NULL); free(pem_public_key_body); return rsa; } ``` 注意:上面的代码使用了OpenSSL库来完成PEM格式的公钥转换,如果你要使用其他库,请相应地修改代码。另外,代码中使用了C语言的标准库函数和动态内存分配函数,你也可以使用其他方式来完成这项工作。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值