2012-03-19 4 views
7

एक बहुत तेजी से रास्ते में किसी पूर्णांक पर एक आईपी धर्मान्तरित के लिए निम्न कोड:मैं इंट-आधारित आईपी पते को स्ट्रिंग में वापस बदलने के लिए बिटशफ्टिंग का उपयोग कैसे करूं?

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

source

प्रश्न

मैं वापस एक के लिए मूल्य कन्वर्ट करने के लिए स्थानांतरण बिट का उपयोग कैसे करूँ आईपी ​​पता?

+0

के संभावित डुप्लिकेट [कैसे एक छोटे endian बाइट सरणी के लिए एक पूर्णांक कन्वर्ट करने के लिए?] (Http://stackoverflow.com/questions/2350099/how-to-convert-an-int-to-a- थोड़ा-एंडियन-बाइट-सरणी) –

+0

@ ओली क्रिसल्सवर्थ मैंने 'नया आईपीएड्रेस (बिटकॉन्टर.गेटबाइट्स (i))' की कोशिश की है, लेकिन इसके वर्तमान दृष्टिकोण की तुलना में एक अलग एंडियन प्रारूप की आवश्यकता है – LamonteCristo

+1

एक पूर्ण कार्यशील उत्तर यहां उपलब्ध है http: //stackoverflow.com/a/9775647/328397 – LamonteCristo

उत्तर

4

प्रयास करें निम्नलिखित

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; 
} 

या एक अत्यधिक से बचने के लिए आउट पैरामीटर की संख्या, 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; 
} 

सामान्य प्रश्न का उपयोग करें : byte के बजाय आप int का उपयोग क्यों कर रहे हैं?

+0

मुझे कक्षा सी सबनेट श्रेणियों में आईपी पता बढ़ाने का इरादा है, लेकिन मैं खाता नहीं लेना चाहता कोड में प्रत्येक ऑक्टेट में 255 के लिए। – LamonteCristo

3

ऊपर अपने कोड मान लिया जाये कि सही, बस नकली बिट्स ड्रॉप करने 0xFF के साथ थोड़ा-परिवर्तन और और परिणाम रिवर्स है:

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

धन्यवाद 0xff मेरी समस्या का हिस्सा है। मुझे सीखना है कि यह एक विशेष चरित्र क्यों है। मैं शर्त लगाऊंगा इसका मतलब है ईओएफ या शून्य। – LamonteCristo

+0

@ makerofthings7 '0xFF' एक बाइट का प्रतिनिधित्व करता है जहां सभी बिट्स '1 पर सेट होते हैं। 'और 0xFF' यह सुनिश्चित करता है कि कम से कम महत्वपूर्ण बाइट के अलावा अन्य सभी चीज़ों को' 0' – JaredPar

+0

पर सेट किया गया है, मैं समझता हूं कि – LamonteCristo

1

पूर्णता के लिए (और समुदाय को वापस देने के तरीके के रूप में) इस प्रकार आईपी किसी सूची में श्रेणी को परिवर्तित करने का तरीका है।

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); 
     } 

    } 
} 
संबंधित मुद्दे