题目1475:IP数据包解析

#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std; 

 int ston(char *str,int l,int r){
	int sum = 0;
	for(int i = l;i < r; i++)
		if(str[i] >= '0' && str[i] <= '9')
		sum = sum * 16 + (str[i] - '0');
		else
		sum = sum * 16 + (str[i] - 'a' + 10);
	return sum;
}


int main()
{

	FILE *fin = fopen("in.txt", "r");
	int n;
	char str[200];
	while(fgets(str,199,fin)){
		cout << str << endl;
		char lenth[10];
		char source[20];
		char dst[20];
		char sp[20];
		char dp[20];
		for(int i = 6,j = 0 ;i <= 10; i++)
			if(str[i] != ' ')
				lenth[j++] = str[i];
		lenth[4] = '\0';

		for(int i = 36,j = 0 ;i <= 46; i++)
			if(str[i] != ' ')
				source[j++] = str[i];
		source[8] = '\0';

		for(int i = 48,j = 0 ;i <= 58; i++)
			if(str[i] != ' ')
				dst[j++] = str[i];
		dst[8] = '\0';

		for(int i = 48,j = 0 ;i <= 58; i++)
			if(str[i] != ' ')
				dst[j++] = str[i];
		dst[8] = '\0';

		for(int i = 60,j = 0 ;i <= 64; i++)
			if(str[i] != ' ')
				sp[j++] = str[i];
		sp[4] = '\0';

		for(int i = 66,j = 0 ;i <= 70; i++)
			if(str[i] != ' ')
				dp[j++] = str[i];
		dp[4] = '\0';

		cout << "Case #1" << endl;
		cout << "Total length = " << ston(lenth,0,4) << " bytes" << endl;
		cout << "Source = " ;
		cout << ston(source,0,2) << "." << ston(source,2,4) << "." << ston(source,4,6) << "." << ston(source,6,8) << endl;
		cout << "Destination = " ;
		cout << ston(dst,0,2) << "." << ston(dst,2,4) << "." << ston(dst,4,6) << "." << ston(dst,6,8) << endl;
		
		cout << "Source Port = " << ston(sp,0,4) << endl;
		cout << "Destination Port = " << ston(dp,0,4) << endl;
	}
	fclose(fin);
	system("pause");
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值