#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
void hexToBinary(int a);
void dataExchange(int a[]);
int datas[8];
int main()
{
hexToBinary(0xd1);
int a[] = { 0xd1,0x01,0x0F,0x54,0x02,0x65,0x6E, 0x68,0x65,0x6c,0x6c,0x6F,0x2c,0x77,0x6F,0x72 ,0x6c ,0x64 ,0x21 };
int b[] = { 0xD1 ,0x01 ,0x09 ,0x55 ,0x01 ,0x2E,0x6E ,0x66 ,0x63 ,0x2E ,0x63 ,0x6F ,0x6D };
dataExchange(a);
cout << "---------------------------------------------" << endl;
dataExchange(b);
}
//-------------将hex转二进制值
void hexToBinary(int a)
{
int n = 8;
int sn = -1;
for (int i = n - 1; i >= 0; i--)
{
int c = (a & 1 << i) >> i;
if (c == 1)
{
sn = 1;
}
if (sn == 1)
{
datas[n - i - 1] = c;
}
}
}
//-----------数据处理
void dataExchange(int a[]) //------------- d1 01 0F 54 02 65 6E 68 65 6c 6F 2c 77 6F 72 6c 64 21
{
hexToBinary(a[0]); //得到datas 0-7 分别对应 mb me cf sr i l inf
int len = -1;
if (datas[0] == 1 && datas[1] == 1)
{
if (datas[2] == 1)
{
cout << "分块情况暂不考虑!" << endl;
return;
}
if (datas[3] == 1) //--------payload长度
{
len = 1;
}
else
{
len = 4;
}
if (datas[4] == 1 || datas[5] == 1)
{
cout << "有 id 字段或 id length 字段的情况不考虑!";
return;
}
if (datas[7] == 1) //----------------tnf
{
//---------判断是网址型还是文本型
int charLen1 = 2 + len; //------3
char s = a[charLen1] - '\0';
if (s == 'U')
{
string s;
switch (a[charLen1 + 1])
{
case 0:
break;
case 1:
s = "http://www";
break;
case 2:
s = "https://www";
break;
case 3:
s = "http://";
break;
case 4:
s = "https://";
break;
case 5:
s = "tel:";
break;
case 6:
s = "mailto:";
break;
case 7:
s = "htp://anonymous:anonymous@";
break;
case 8:
s = "ftp://ftp.";
break;
case 9:
s = "ftps://";
break;
}
int start = charLen1 + 2;
int dataLen = a[2] - 1;
cout << "该数据为网址型,内容为:" << s;
for (int i = start; i < start + dataLen; i++)
{
char temp = a[i] - '\0';
cout << temp;
}
cout << endl;
}
if (s == 'T')
{
//-----------语言码长度
int languageLen = a[charLen1 + 1]; //-------------4 02
int start = charLen1 + 2 + languageLen; // -----3 2
int dataLen = a[2] - (1 + languageLen);
//---------语言码内容
int s = charLen1 + 1 + 1;
cout << "该数据为文本型,语言码内容为:";
for (int i = s; i < s + languageLen; i++)
{
char temp = a[i] - '\0';
cout << temp;
}
cout << endl;
cout << "实际内容为:";
for (int i = start; i < dataLen + start; i++)
{
char temp = a[i] - '\0';
cout << temp;
}
cout << endl;
}
}
}
}
|