0

I need to read computers ip adress, which is already done. After that I need to change the recieved ip value to hex and then to decimal, basically like this

127.0.0.1 flip the value to 1.0.0.127 to hex 0100007F and finally to 16777343.

public static string GetLocalIPAddress()
    {
        var host = Dns.GetHostEntry(Dns.GetHostName());
        foreach (var ip in host.AddressList)
        {
            if (ip.AddressFamily == AddressFamily.InterNetwork)
            {

                string hexValue = ip.ToString();
                string cleanAmount = hexValue.Replace(".", string.Empty);
                Console.Write(cleanAmount + "\n");
                return ip.ToString();
            }
        }
        throw new Exception("No network adapters with an IPv4 address in the system!");
    }


    static void Main(string[] args)
    {
        GetLocalIPAddress();
    }
Michał Turczyn
  • 32,028
  • 14
  • 47
  • 69
Fuppetia
  • 47
  • 4
  • 1
    What issue you are facing with this code? – Chetan Oct 01 '18 at 09:46
  • 1
    the `ip.ToString()` returns a "dotted decimal" value, not "hex". By removing the `.` you loose the distinction between "1.21" and "12.1" – Hans Kesting Oct 01 '18 at 09:51
  • Firstly you need to define what your expected behaviour is e.g. 100172 in hex is 1871F. 1000000127 is 3B9ACA7F. 1 in hex is 1, 0 is 0, 127 is 7F. If you want to treat the octets individually (which it seems you possibly do?) then it is best to separate them whilst they are delimited rather than remove the delimiter – Dave Oct 01 '18 at 09:52
  • There are plenty of good answers here, best pick your favorite – TheGeneral Oct 03 '18 at 09:41

5 Answers5

4

Here you go

string input = "127.0.0.1";
string hex = string.Concat(input.Split('.').Reverse().Select(x => int.Parse(x).ToString("X").PadLeft(2,'0'))); // 0100007F 
int result = Convert.ToInt32(hex, 16); //16777343
fubo
  • 44,811
  • 17
  • 103
  • 137
0

You can use Convert.ToString to convert an int from decimal to a different numeral system:

Convert.ToString(127, 16); //16 for hexadecimal
Martin Zikmund
  • 38,440
  • 7
  • 70
  • 91
0

Why do you want to reverse anything at all?
You could just read each group and multiply its value by 256^rank:

static void Main()
{
    string ip = "127.0.0.1";

    string[] splitted = ip.Split('.');

    int value = 0;
    int factor = 1;

    for(int i=0;i<splitted.Length;i++)
    {
        value += factor*Convert.ToInt32(splitted[i]);
        factor*=256;
    }

    Console.WriteLine(value);
}

Writes 16777343.

Rafalon
  • 4,450
  • 2
  • 16
  • 30
0

Try this:

string ipAddress = "127.0.0.1";
string[] parts = ipAddress.Split('.');
string hexResult = "";
// loop backwards, to reverese order of parts of IP
for (int i = parts.Length - 1; i >= 0; i--)
    hexResult += int.Parse(parts[i]).ToString("X").PadLeft(2, '0');
int finalResult = Convert.ToInt32(hexResult, 16);
Michał Turczyn
  • 32,028
  • 14
  • 47
  • 69
0

It depends why you want this reversed, and if this is an endian issue

essentially you just need to return your int

BitConverter.ToInt64(IPAddress.Parse("127.0.0.1").GetAddressBytes(), 0);

Or a more verbose example

var bytes = IPAddress.Parse("127.0.0.1").GetAddressBytes();

if (!BitConverter.IsLittleEndian)
   Array.Reverse(bytes);

for (int i = 0; i < bytes.Length; i++)
    Console.Write(bytes[i].ToString("X2"));

Console.WriteLine("");

long r = BitConverter.ToInt32(bytes, 0);
Console.WriteLine(r);

Output

7F000001 
16777343

Full Demo Here

TheGeneral
  • 79,002
  • 9
  • 103
  • 141
  • IP addresses are in network byte order i.e. big endian, so to convert to an IPv4 address to a long that should be if (BitConverter.IsLittleEndian) – Ananke Oct 01 '18 at 10:07
  • for some reason if I use `Array.Reverse(bytes)` it returns me a -1062728671 with my computers own ip, if not reversed returns 554477760 – Fuppetia Oct 02 '18 at 07:38
  • @Fuppetia Make sure you use long, and not int – TheGeneral Oct 02 '18 at 07:40