字符 回调 字节相互转换 身份证usc-2转码

#define __MAX_MSGSIZE__ 1024
std::string MsgSpliceStr(char * fmt,...)
{
	char Msgbuf[__MAX_MSGSIZE__];	
	va_list arg;
	va_start(arg, fmt);
	vsprintf(Msgbuf, fmt, arg);
	return std::string(Msgbuf);
}

//用法
int _tmain(int argc, _TCHAR* argv[])
{


	int tmlen = 30;
	int sendlcou = 20;
	std::string strdk = MsgSpliceStr("--send length %d-- 第 %d 条\n\n",tmlen,sendlcou);
	printf("%s", strdk.c_str());
}
下面是回调的用法
typedef void (WINAPI * MsgCALLBACK)(const char * pParam);

class CardReaderMgr
{
public:
	static void WINAPI DebugMsgCallBack(const char * pParam);
};

void WINAPI CardReaderMgr::DebugMsgCallBack(const char * pParam)
{
	printf("%s \n",pParam);
}

//用法
int _tmain(int argc, _TCHAR* argv[])
{
	CDZS_MsgCALLBACK museMsg = CardReaderMgr::DebugMsgCallBack;
	museMsg("haha");
}

多字节与Unicode相互转换

std::string  wstring_to_string(const std::wstring& wstr)
{
	std::string  str;
	if(wstr.empty())
	{
		return str;
	}

	int istrLen = WideCharToMultiByte(CP_ACP,0,wstr.c_str(),-1,NULL,0,NULL,NULL);

	LPSTR lpsBuf = new(std::nothrow) CHAR[istrLen];
	if(NULL == lpsBuf)
	{
		return str;
	}

	(void)memset(lpsBuf,0,istrLen * sizeof(CHAR));
	int nResult = WideCharToMultiByte(CP_ACP,0,wstr.c_str(),-1,lpsBuf,istrLen,NULL,NULL);	
	if(0 == nResult)
	{ 
		delete []lpsBuf;
		return str;
	}

	str = lpsBuf;
	delete []lpsBuf;
	return  str;
}

std::wstring string_to_wstring(const std::string& str)
{
	std::wstring  wstr;
	if(str.empty())
	{
		return wstr;
	}

	int iWstrLen =  MultiByteToWideChar(CP_ACP,0,str.c_str(),-1,NULL,0);
	LPWSTR lpwsBuf = new(std::nothrow) WCHAR[iWstrLen];
	if(NULL == lpwsBuf)
	{
		return wstr;
	}

	(void)memset(lpwsBuf,0,iWstrLen * sizeof(WCHAR));
	int nResult = MultiByteToWideChar(CP_ACP,0,str.c_str(),-1,lpwsBuf,iWstrLen);

	if(0 == nResult)
	{ 
		delete []lpwsBuf;
		return wstr;
	}

	wstr = lpwsBuf;
	delete []lpwsBuf;
	return  wstr;
}
//用法
int _tmain(int argc, _TCHAR* argv[])
{

	std::wstring mwstr = L"我是unicode字节";
	std::string  msts = wstring_to_string(mwstr);
	printf("%s", msts.c_str());

	std::string mstr = "我是多字节";
	std::wstring mwstr2 = string_to_wstring(mstr);
	

}

身份证usc-2转码显式

std::wstring Transfer2QStr(std::string &qb_buffer)
{
	unsigned char buffer=0x00;
	unsigned short w_buffer=0x0000;
	std::wstring qs_buffer;
	qs_buffer.resize(qb_buffer.length()/2);
	for(int i=0,j=0;i<qb_buffer.length()/2;i++)
	{
		w_buffer=0x0000;
		buffer=qb_buffer[j];
		w_buffer=buffer;
		j++;
		buffer=qb_buffer[j];
		w_buffer |= buffer << 8;
		j++;
		qs_buffer[i]=w_buffer;
	}

	return qs_buffer;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值