????? 昨天一个偶然机会,看到一篇文章,是关于C++ socket server和client传输报文的。
????? 然后就联想到以前做的报文传输,就联想到把十六进制流转换为二进制制流进行传输,最大的好处就是减少网络传输
的负载。
????? 例如,”1234ab“字符串一般存储占用6个byte,可是转换为 0x12 0x34 0xab,这样就只需要3个byte,一样达到
预期的效果,现在就不废话了,把写的测试代码贴出来,有兴趣的,一起看看。
?
?
/*! *********************************************************************/
/*! \brief convert int to binary buffer
\param[in] value
\param[out] str
\Example 11 => "1011"
/*************************************************************************/
const char* IntToBin(unsigned int value, string& str)
{
str.empty();
string temp;
byte binval =0x00;
do
{
if ( value == 1)
{
temp.append("1");
binval |= 0x08;
string::reverse_iterator itr = temp.rbegin();
string::reverse_iterator end = temp.rend();
for (; itr != end ;++itr)
{
str += *itr;
}
return str.c_str();
}
else if ( value %2 == 0)
{
value /= 2;
temp.append("0");
binval >>= 1;
}
else
{
value /= 2;
temp.append("1");
binval |= 0x08;
binval >>= 1;
}
}
while(true);
}
void IntToBinTest()
{
string str;
cout << IntToBin(11, str) << endl ;
cout << IntToBin(8, str) << endl ;
cout << IntToBin(1234567, str) << endl ;
}
//////////////////////////////////////////////////////////////////////////
?
?
/*! *********************************************************************/
/*! \brief convert int to binary buffer
\param[in] value
\Example 11 => "1011"
/*************************************************************************/
void IntToBin2(unsigned long value)
{
//byte ibyte =255;
while(value)
{
if (value & 0x01)
{
cout<<1;
}
else
{
cout<<0;
}
value >>= 1;
}
cout<<endl;
}
void IntToBin2Test()
{
IntToBin2(1234567);
}
//////////////////////////////////////////////////////////////////////////
?
?
/*! *********************************************************************/
/*! \brief check the character is valid hex char('0'~'9', 'A'~'z', 'a'~'z')
\param[in] ch
\param[out] binVal
\Example 'a' => 0x0a
/*************************************************************************/
bool isValidHexChar(char ch, byte& binVal)
{
if (ch >= '0' && ch <= '9')
{
binVal = ch - 48;
return true;
}
else if (ch>='A' && ch <= 'F')
{
binVal = ch - 55;
return true;
}
else if (ch>='a' && ch <= 'f')
{
binVal = ch - 87;
return true;
}
return false;
}
void isValidHexCharTest()
{
for (int i = '0'; i <= 'z'; i++)
{
byte val = 0;
isValidHexChar(i, val);
}
}
//////////////////////////////////////////////////////////////////////////
?
/*! *********************************************************************/
/*! \brief convert two hex characters to byte
\param[in] ch_h
\param[in] ch_l
\param[out] binval
\Example '1a' => 0x1a
/*************************************************************************/
void HexBufConvertToByte (char ch_h, char ch_l, byte& binval)
{
byte temp = 0x00;
binval = 0x00;
if (isValidHexChar(ch_h, temp))
{
if ( temp & 0x08 ) binval |= 0x80;
if ( temp & 0x04 ) binval |= 0x40;
if ( temp & 0x02 ) binval |= 0x20;
if ( temp & 0x01 ) binval |= 0x10;
}
if (isValidHexChar(ch_l, temp))
{
if ( temp & 0x08 ) binval |= 0x08;
if ( temp & 0x04 ) binval |= 0x04;
if ( temp & 0x02 ) binval |= 0x02;
if ( temp & 0x01 ) binval |= 0x01;
}
}
void HexBufConvertToByteTest()
{
byte binval = 0x00;
for (int i = '0'; i <= 'z'; i++)
{
HexBufConvertToByte(i, i+1, binval);
}
}
//////////////////////////////////////////////////////////////////////////
/*! *********************************************************************/
/*! \brief check hex characters byte buffer
\param[in] hexBuf
\param[in] binBuffer
\param[in] iBinbufLen
\Example "01234567" => 0x01 0x23 0x45 0x67
/*************************************************************************/
void HexBufConvertToBin(const char* hexBuf, BYTE* binBuffer, int iBinbufLen)
{
int iHexsize = strlen(hexBuf);
for ( int i = 0; i < (iHexsize/2); i++)
{
if ( i < iBinbufLen)
{
HexBufConvertToByte( hexBuf[i*2], hexBuf[i*2+1], *(binBuffer++) );
}
}
}
void HexBufConvertToBinTest()
{
const char* pHexBuf ="0123456789ABCDEFabcdef";
byte bBuffer[100] ={0};
HexBufConvertToBin(pHexBuf, bBuffer, sizeof(bBuffer));
}
//////////////////////////////////////////////////////////////////////////
?
??? 打完收工。~_~
?