2012-03-19 19 views
7

Các mã sau đây để chuyển đổi một IP đến một int một cách rất nhanh:Làm cách nào để sử dụng BitShifting để chuyển đổi địa chỉ IP int thành chuỗi?

static int ipToInt(int first, int second, int third, int fourth) 
    { 
     return (first << 24) | (second << 16) | (third << 8) | (fourth); 
    } 

source

Câu hỏi

Làm thế nào để sử dụng chút thay đổi để chuyển đổi các giá trị trở lại một Địa chỉ IP?

+0

bản sao có thể có của [Cách chuyển đổi một int thành một mảng byte cuối cùng?] (Http://stackoverflow.com/questions/2350099/how-to-convert-an-int-to-a- little-endian-byte-array) –

+0

@OliCharlesworth Tôi đã thử 'IPAddress mới (BitConverter.GetBytes (i))' nhưng yêu cầu một định dạng cuối khác với cách tiếp cận hiện tại của tôi cung cấp – LamonteCristo

+1

Câu trả lời làm việc hoàn chỉnh có sẵn tại đây http: //stackoverflow.com/a/9775647/328397 – LamonteCristo

Trả lời

4

Hãy thử như sau

static out intToIp(int ip, out int first, out int second, out int third, out int fourth) { 
    first = (ip >> 24) & 0xFF; 
    second = (ip >> 16) & 0xFF; 
    third = (ip >> 8) & 0xFF; 
    fourth = ip & 0xFF; 
} 

Hoặc để tránh quá mức số tham số ngoài, sử dụng struct

struct IP { 
    int first; 
    int second; 
    int third; 
    int fourth; 
} 

static IP intToIP(int ip) { 
    IP local = new IP(); 
    local.first = (ip >> 24) & 0xFF; 
    local.second = (ip >> 16) & 0xFF; 
    local.third = (ip >> 8) & 0xFF; 
    local.fourth = ip & 0xFF; 
    return local; 
} 

Câu hỏi chung : Tại sao bạn sử dụng int tại đây thay vì byte?

+0

Tôi dự định tăng địa chỉ IP trên phạm vi subnet lớp C, nhưng tôi không muốn tài khoản cho 255 trong mỗi octet trong mã. – LamonteCristo

3

Giả sử mã của bạn ở trên là chính xác, chỉ cần đảo ngược các bit-ca và AND kết quả với 0xFF để thả bit giả mạo:

first = (ip >> 24) & 0xff; 
second = (ip >> 16) & 0xff; 
third = (ip >> 8) & 0xff; 
fourth = ip & 0xff; 
+0

Cảm ơn 0xff là một phần của vấn đề của tôi. Tôi cần phải tìm hiểu tại sao đó là một nhân vật đặc biệt. Tôi sẽ đặt cược nó có nghĩa là EOF hoặc null. – LamonteCristo

+0

@ makerofthings7 '0xFF' đại diện cho một byte duy nhất trong đó tất cả các bit được đặt thành' 1. '& 0xFF' đảm bảo rằng mọi thứ khác với byte ít quan trọng nhất được đặt thành' 0' – JaredPar

+0

Ahh tôi thấy, điều đó có ý nghĩa – LamonteCristo

1

Để hoàn thành (và là cách để trả lại cho cộng đồng), đây là cách chuyển đổi dải IP thành danh sách.

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Net; 

namespace NormalizeIPRanges 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 

      if (!BitConverter.IsLittleEndian) 
       // http://stackoverflow.com/a/461766/328397 
       throw new NotSupportedException ("This code requires a little endian CPU"); 

      // IPv4 
      string input = "64.233.187.98 - 64.233.188.2"; 
      var ipRange = input.Replace(" ", "").Split("-".ToCharArray()); 

      if (ipRange.Length == 2) 
      { 
       var startBytes =IPAddress.Parse(ipRange[0]).GetAddressBytes(); 
       var stopBytes = IPAddress.Parse(ipRange[1]).GetAddressBytes(); 

       if (startBytes.Length != 4 || stopBytes.Length != 4) 
       { 
        // Note this implementation doesn't imitate all nuances used within MSFT IP Parsing 
        // ref: http://msdn.microsoft.com/en-us/library/system.net.ipaddress.parse.aspx 

        throw new ArgumentException("IP Address must be an IPv4 address"); 
       } 

       // IP addresses were designed to do bit shifting: http://stackoverflow.com/a/464464/328397 
       int startAddress = ipToInt(startBytes[0], startBytes[1], startBytes[2], startBytes[3]); 
       var t =intToIP(startAddress); 

       int stopAddress = ipToInt(stopBytes[0], stopBytes[1], stopBytes[2], stopBytes[3]); 
       var tr = intToIP(stopAddress); 


       for (int i = startAddress; i <= stopAddress; i++) 
       { 
        Console.WriteLine(intToIP(i)); 
       } 
      } 
     } 

     static int ipToInt(int first, int second, int third, int fourth) 
     { 
      return (first << 24) | (second << 16) | (third << 8) | (fourth); 
     } 
     static string intToIP(int ip) 
     { 
      var a = ip >> 24 & 0xFF; 
      var b = ip >> 16 & 0xFF; 
      var c = ip >> 8 & 0xFF; 
      var d = ip & 0xFF; 

      return String.Format("{0}.{1}.{2}.{3}",a,b,c,d); 
     } 

    } 
}