-1

I need a logic how to convert hexadecimal to IP(IPV4) address for example

6F6F6F6F(HEXADECIMAL) - 111.111.111.111(IPADDRESS)
FFFFFFFF(HEXADECIMAL) - 255.255.255.255(IPADDRESS)
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
satya
  • 1
  • 5

3 Answers3

2

Hi the same question in this Link:

C# convert hex into ip

If the values represent IPv4 addresses you can use the long.Parse method and pass the result to the IPAddress constructor:

var ip = new IPAddress(long.Parse("4a0e94ca", NumberStyles.AllowHexSpecifier));
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
1

You can try using Linq to query the original string. The query can be

  1. Split original string into chunks of size 2
  2. Convert each chunk into int value
  3. Join all these int's into the final string
using System.Linq;

...

string source = "6F6F6F6F";

string address = string.Join(".", source
  .Chunk(2)
  .Select(chunk => Convert.ToInt32(new string(chunk), 16)));

Demo:

using System.Linq;

...

static string MyConvert(string source) => string.Join(".", source
  .Chunk(2)
  .Select(chunk => Convert.ToInt32(new string(chunk), 16)));

string[] tests = {
  "6F6F6F6F",
  "FFFFFFFF",
};

string result = string.Join(Environment.NewLine, tests
  .Select(test => $"{test}(HEXADECIMAL) - {MyConvert(test)}(IPADDRESS)"));

Console.Write(result);

Outcome:

6F6F6F6F(HEXADECIMAL) - 111.111.111.111(IPADDRESS)
FFFFFFFF(HEXADECIMAL) - 255.255.255.255(IPADDRESS)

Edit: Reverse can be Linq based as well:

  1. We split the source by .
  2. Parse each item to int and format as hexadecimal with 2 mandatory digits
  3. Concat all these chunks into the result string
string source = "123.45.67.221";

string result = string.Concat(source
  .Split('.')
  .Select(item => int.Parse(item).ToString("X2")));

Edit 2: You can use GroupBy instead of Chunk in case of old .net versions:

string address = string.Join(".", source
  .Select((item, index) => new { item = item, group = index / 2 })
  .GroupBy(pair => pair.group, pair => pair.item)
  .Select(group => Convert.ToInt32(new string(group.ToArray()), 16)));

Fiddle

Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
  • sir for ipaddress to hexadecimal conversion also can we use linq and make it short rather writing for loop if so could u share in linq how we can implement ipaddress to hexadecimal conversion – satya Mar 21 '23 at 15:10
  • @satya: Yes, we can use Linq for reverse (IP Address to Hexadecimal) as well; I've edited the answer – Dmitry Bychenko Mar 21 '23 at 15:16
  • thank u so much sir for all the support but iam facing some issue with chunk it is not supporting i used namespace linq too any other alternate method using linq – satya Mar 22 '23 at 02:45
  • sir am getting string does not contain definition for chunk am i missing anything or can u suggest any alternate – satya Mar 22 '23 at 06:43
  • @satya: `Chunk` is a Linq *extension method*, please fiddle: https://dotnetfiddle.net/9U2Z1w – Dmitry Bychenko Mar 22 '23 at 08:52
  • checked sir in .net framework chunk is not supporting quite a old project is there any alternate for chunk – satya Mar 22 '23 at 09:53
  • @satya: you can use `GroupBy` instead of `Chunk` (please, see my 2nd edit) – Dmitry Bychenko Mar 22 '23 at 09:58
0

You can just roll a simple function that gets the job done, like this:

public string ToDecimalIp(ReadOnlySpan<char> hexIp)
{
    return @$"{byte.Parse(hexIp.Slice(0, 2), System.Globalization.NumberStyles.HexNumber)}.
        {byte.Parse(hexIp.Slice(2, 2), System.Globalization.NumberStyles.HexNumber)}.
        {byte.Parse(hexIp.Slice(4, 2), System.Globalization.NumberStyles.HexNumber)},
        {byte.Parse(hexIp.Slice(6, 2), System.Globalization.NumberStyles.HexNumber)}";
}

Of course you need to take care of input validation if you're not in control, but I leave that up to you.

roten
  • 196
  • 13