-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7765fa7
commit 341f083
Showing
2 changed files
with
34 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,39 @@ | ||
using System.Text; | ||
// Copyright @ 2019 矿冶园 | ||
// Product: 实验室安全手机版 | ||
// Subsystem: Danmaku | ||
// Author: 迟竞雷 | ||
// Time: 2019-07-24 8:54 | ||
// Description: | ||
|
||
using System.Net; | ||
using System.Numerics; | ||
|
||
namespace Danmaku.Utils | ||
{ | ||
public class Ip | ||
{ | ||
/// <summary> | ||
/// Ip转化为10进制 | ||
/// </summary> | ||
/// <param name="ip">点分隔的ip</param> | ||
/// <returns>十进制ip</returns> | ||
public static long Ip2Long(string ip) | ||
{ | ||
if (string.IsNullOrWhiteSpace(ip)) return 0; | ||
char[] separator = {'.'}; | ||
var items = ip.Split(separator); | ||
return (long.Parse(items[0]) << 24) | ||
| (long.Parse(items[1]) << 16) | ||
| (long.Parse(items[2]) << 8) | ||
| long.Parse(items[3]); | ||
} | ||
|
||
/// <summary> | ||
/// 十进制ip转换为字符串 | ||
/// </summary> | ||
/// <param name="ipInt">十进制ip</param> | ||
/// <returns>字符串ip</returns> | ||
public static string Long2Ip(long ipInt) | ||
{ | ||
/// <summary> | ||
/// Ip转化为10进制 | ||
/// </summary> | ||
/// <param name="ip">点分隔的ip</param> | ||
/// <returns>十进制ip</returns> | ||
public static BigInteger Ip2Long(string ip) | ||
{ | ||
if (string.IsNullOrWhiteSpace(ip)) return 0; | ||
if (IPAddress.TryParse(ip, out var a)) | ||
return new BigInteger(a.GetAddressBytes()); | ||
return 0; | ||
} | ||
|
||
/// <summary> | ||
/// 十进制ip转换为字符串 | ||
/// </summary> | ||
/// <param name="ipInt">十进制ip</param> | ||
/// <returns>字符串ip</returns> | ||
public static string Long2Ip(BigInteger ipInt) | ||
{ | ||
var sb = new StringBuilder(); | ||
sb.Append((ipInt >> 24) & 0xFF).Append("."); | ||
sb.Append((ipInt >> 16) & 0xFF).Append("."); | ||
sb.Append((ipInt >> 8) & 0xFF).Append("."); | ||
sb.Append(ipInt & 0xFF); | ||
return sb.ToString(); | ||
var a = ipInt.ToByteArray(); | ||
return new IPAddress(a).ToString(); | ||
} | ||
} | ||
} |