0
A
回答
0
它使用位操作很簡單:
byte[] ip1 = {1, 1, 1, 0};
byte[] ip2 = {1, 1, 1, 255};
byte[] omask =
{
(byte)(ip1[0]^ip2[0]),
(byte)(ip1[1]^ip2[1]),
(byte)(ip1[2]^ip2[2]),
(byte)(ip1[3]^ip2[3])
};
string mask = Convert.ToString(omask[0], 2) + Convert.ToString(omask[1], 2)
+ Convert.ToString(omask[2], 2) + Convert.ToString(omask[3], 2);
// count the number of 1's in the mask. Substract to 32:
// NOTE: the mask doesn't have all the zeroes on the left, but it doesn't matter
int bitsInMask = 32 - (mask.Length - mask.IndexOf("1")); // this is the /n part
byte[] address =
{
(byte)(ip1[0] & ip2[0]),
(byte)(ip1[1] & ip2[1]),
(byte)(ip1[2] & ip2[2]),
(byte)(ip1[3] & ip2[3])
};
string cidr = address[0] + "." + address[1] + "."
+ address[2] + "." + address[3] + "/" + bitsInMask;
CIDR給你網絡r ANK。
相關問題
- 1. 將IP地址轉換爲IP範圍
- 2. Bourne shell - 將IP範圍轉換爲一組子網
- 3. 轉換IP範圍的IP地址
- 4. 紅寶石:如何將IP範圍轉換爲IP的陣列
- 5. Python IP範圍到IP範圍匹配
- 6. Apache重寫子網IP範圍
- 7. 從IP範圍計算子網掩碼
- 8. 匹配IP地址與IP範圍?
- 9. 變換IP地址,IP地址範圍(起始IP - 結束IP)
- 10. 轉換IP範圍列表來CIDR
- 11. MySQL查詢CIDR轉換成IP範圍
- 12. Python - 將IP地址列表轉換爲地址範圍列表
- 13. 如何使用Ruby將IP範圍轉換爲單個CIDR
- 14. C#如何將IP範圍轉換爲字符串數組?
- 15. 計算IP的IP範圍
- 16. IP到CIDR/IP範圍
- 17. 將IP轉換爲反向ip查找
- 18. 合併IP範圍與PHP
- 19. 如何將CIDR轉換爲C#中的網絡和IP地址範圍?
- 20. IP pinging的範圍
- 21. python和ip範圍?
- 22. iOS iP地址比較IP範圍
- 23. CSV讀寫&IP匹配IP範圍
- 24. PHP從IP範圍生成IP列表
- 25. IP範圍從和IP範圍到 - 動態文本框
- 26. Windows Azure網站 - 阻止IP範圍
- 27. 網絡中主機IP的範圍
- 28. PHP IP到網絡轉換
- 29. 來自IP地址/網絡掩碼使用VBScript的IP範圍電纜因子
- 30. 爆炸給定的IP範圍與PHP
並非所有的範圍都會有適當的CIDR網絡掩碼;你打算如何處理這些? – sarnold
來澄清:您期望爲1.1.1.3 - 1.1.1.5輸出什麼? – Vlad
作爲參考,這與您所要求的相反:http://stackoverflow.com/questions/1470792/how-to-calculate-the-ip-range-when-the-ip-address-and -the-網絡掩碼,被賜予 – BeemerGuy