⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mplocator.cs

📁 这是一个手机归属第的查询
💻 CS
字号:
/************************************************************
IP数据库、手机归属地查询软件完整源代码(C#)
  Author: rssn
  Email : rssn@163.com
  QQ    : 126027268
  Blog  : http://blog.csdn.net/rssn_net/
 ************************************************************/
using System;
using System.IO;

namespace Hebust
{
	public class MpLocator
	{
		//查询号码,返回号码段和归属地信息
		public static MpLocation GetMpLocation(string fn, int num)
		{
			if(!File.Exists(fn))
			{
				throw new Exception("文件不存在!");
			}
			FileStream fs=new FileStream(fn,FileMode.Open,FileAccess.Read,FileShare.Read);
			BinaryReader fp=new BinaryReader(fs);
			
			//读文件头,获取首末记录偏移量
			int fo=fp.ReadInt32();
			int lo=fp.ReadInt32();
			int rcOffset=getIndexOffset(fs,fp,fo,lo,num);
			MpLocation mpl;
			if(rcOffset>=0)
			{
				fs.Seek(rcOffset,System.IO.SeekOrigin.Begin);
				//读取号码段起始地址和结束地址
				mpl.NumStart=fp.ReadInt32();
				mpl.NumEnd=fp.ReadInt32();
				//如果查询的号码处于中间空段
				if(num>mpl.NumEnd)
				{
					mpl.NumStart=0; mpl.NumEnd=0;
					mpl.Location="未知地址";
				}
				else
				{
					//读取字符串偏移量,3字节!
					int lstrOffset=ReadInt24(fp);
					fs.Seek(lstrOffset,System.IO.SeekOrigin.Begin);
					//读取归属地字符串
					mpl.Location=ReadString(fp);
				}
			}
			else
			{
				//没找到记录
				mpl.NumStart=0; mpl.NumEnd=0;
				mpl.Location="未知地址";
			}
			fp.Close();
			fs.Close();
			return mpl;
		}

		//获取号码段记录在文件中的偏移量
		private static int getIndexOffset(FileStream fs, BinaryReader fp, int _fo, int _lo, int num)
		{
			int fo=_fo, lo=_lo;
			int mo;    //中间偏移量
			int mv;    //中间值
			int fv,lv; //边界值
			int llv;   //边界末末值
			fs.Seek(fo,System.IO.SeekOrigin.Begin);
			fv=fp.ReadInt32();
			fs.Seek(lo,System.IO.SeekOrigin.Begin);
			lv=fp.ReadInt32();
			llv=fp.ReadInt32();
			//边界检测处理
			if(num<fv)
				return -1;
			else if(num>llv)
				return -1;
			//使用"二分法"确定记录偏移量
			do
			{
				mo=fo+(lo-fo)/11/2*11;
				fs.Seek(mo,System.IO.SeekOrigin.Begin);
				mv=fp.ReadInt32();
				if(num>=mv)
					fo=mo;
				else
					lo=mo;
				if(lo-fo==11)
					mo=lo=fo;
			} while(fo!=lo);
			return mo;
		}

		// 读取三字节的整数
		private static int ReadInt24(BinaryReader fp)
		{
			if(fp==null) return -1;
			int ret=0;
			ret|=(int)fp.ReadByte();
			ret|=(int)fp.ReadByte()<<8&0xFF00;
			ret|=(int)fp.ReadByte()<<16&0xFF0000;
			return ret;
		}
		// 读取字符串
		private static string ReadString(BinaryReader fbr)
		{
			byte[] TempByteArray=new byte[128];
			int i=0;
			do
			{
				TempByteArray[i]=fbr.ReadByte();
			} while(TempByteArray[i++]!='\0' && i<128);
			return System.Text.Encoding.Default.GetString(TempByteArray).TrimEnd('\0');
		}
	}

	//手机归属地结构体类型
	public struct MpLocation
	{
		public int NumStart;
		public int NumEnd;
		public string Location;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -