Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4175405
  • 博文数量: 291
  • 博客积分: 8003
  • 博客等级: 大校
  • 技术积分: 4275
  • 用 户 组: 普通用户
  • 注册时间: 2010-10-30 18:28
文章分类

全部博文(291)

文章存档

2017年(1)

2013年(47)

2012年(115)

2011年(121)

2010年(7)

分类: Python/Ruby

2011-05-12 23:29:05

  1. <?php
  2. /*
  3.     二进制文件格式如下
  4.     +-----------------------------------------------------+
  5.     |总记录数(4B)|空白(7B)--------------------------------|
  6.     |开始ip(4B) |结束ip(4B) |省id(1B)|市id(1B)|ispid(1B)|
  7.     |开始ip(4B) |结束ip(4B) |省id(1B)|市id(1B)|ispid(1B)|
  8.     |开始ip(4B) |结束ip(4B) |省id(1B)|市id(1B)|ispid(1B)|
  9.     +-----------------------------------------------------+
  10. */
  11. //要写如的文件

  12. $filename="a.dat";
  13. //wb表示用二进制重新写文件

  14. $fp=fopen($filename,'wb')or die("cannot open a.dat");
  15. //原始数据:开始ip,结束ip,省id,市id,isp的id

  16. $records=array(
  17.                   array(
  18.                           '110.25.23.1', //开始ip

  19.                           '110.25.23.254', //结束ip

  20.                           3, //省id

  21.                           1, //市id

  22.                           6 //isp的id

  23.                        ),
  24.                   array(
  25.                           '210.200.10.1',
  26.                           '210.254.23.254',
  27.                           1,
  28.                           1,
  29.                           2
  30.                        ),
  31.                  array(
  32.                           '211.68.154.1',
  33.                           '211.254.47.254',
  34.                           2,
  35.                           2,
  36.                           2
  37.                        ),

  38.                                     array(
  39.                           '222.22.45.1',
  40.                           '254.255.255.255',
  41.                           3,
  42.                           3,
  43.                           3
  44.                        ),
  45.                   array(
  46.                           '255.255.255.255',
  47.                           '255.255.255.255',
  48.                           0,
  49.                           0,
  50.                           0
  51.                        )
  52.               );

  53. //计算记录总数

  54. $total=count($records);
  55. //用4个字节记录记录总数

  56. $inputstr=sprintf("%08x",$total);
  57. echo "$inputstr";
  58. //用4个字节记录记录总数,写入文件

  59. $str=pack ("H8",$inputstr);
  60. fwrite($fp,$str);

  61.  //补充7个字节的空白

  62. $str=pack ("a7","");
  63. fwrite($fp,$str);
  64. foreach($records as $item)
  65. {
  66.     //用4个字节记录开始ip

  67.     $inputstr=sprintf("%x",ip2long($item[0]));
  68.     $str=pack ("H8",$inputstr);
  69.     fwrite($fp,$str);
  70.     
  71.         //用4个字节记录结束ip

  72.     $inputstr=sprintf("%x",ip2long($item[1]));
  73.     $str=pack ("H8",$inputstr);
  74.     fwrite($fp,$str);
  75.     
  76.     //用1个字节记录省id

  77.     $inputstr=sprintf("%02x",$item[2]);
  78.     $str=pack ("H2",$inputstr);
  79.     fwrite($fp,$str);
  80.     
  81.     //用1个字节记录市id

  82.     $inputstr=sprintf("%02x",$item[3]);
  83.     $str=pack ("H2",$inputstr);
  84.     fwrite($fp,$str);
  85.     
  86.         //用1个字节记录ispid

  87.     $inputstr=sprintf("%02x",$item[4]);
  88.     $str=pack ("H2",$inputstr);
  89.     fwrite($fp,$str);
  90. }
  91. fclose($fp);


  92. ?>
阅读(6833) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~