【VC开源代码栏目提醒】:本文主要为网学会员提供WZDSTR.CPP,希望对需要WZDSTR.CPP网友有所帮助,学习一下!
// WzdStr.cpp
//
#include "stdafx.h"
#include "WzdStr.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CWzdString
// extracts characters from beg to end
CString CWzdString::Extract(int beg,int end)
{
return Mid(beg-1,end-beg+1);
}
// stores binary as HEX ascii string
void CWzdString::PutBinary(LPBYTE pByte,int len)
{
Empty();
CString hex;
for (int i=0;i<len;i++)
{
hex.Format("%02X",pByte[i]);
*this+=hex;
}
}
// retrieves HEX ascii string as binary
// returned value is actual binary length
int CWzdString::GetBinary(LPBYTE pByte,int maxlen)
{
// make sure the string contains only valid hex characters
if (SpanIncluding("0123456789aAbBcCdDeEfF") != *this)
{
return 0;
}
// make sure less then max bytes
int len=GetLength();
if (len>maxlen*2)
{
len=maxlen*2;
}
// pad HEX to even number
CString hex=*this;
if ((len % 2) != 0)
{
len++;
hex+="0";
}
// convert to binary
len/=2;
for (int i = 0; i <len; i++)
{
int b;
sscanf(hex.Mid((i * 2), 2), "%02X", &b);
pByte[i] = (BYTE)b;
}
return(len);
}