字符 回调 字节相互转换 身份证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

你可能感兴趣的:(学习平时要查找)