0

I have numbers stored in a SQL Server char(39) field that I need to convert to a IPv6 IPAddress?

Examples

281470698521600
281470698522623
58569107296622255421594597096899477504 
58569107375850417935858934690443427839

I don't know much about IPv6 but I'm assuming I can ping the address to verify it is correct. I could do with some help converting it from a number though? The examples seem short, are they missing subnetting?

2
  • 2
    What have you tried? Where did you get stuck? Commented Mar 21, 2021 at 0:03
  • I've converted bigint to IPv4 addresses using IPAddress ip = new IPAddress(convertedNumber); after check BitConverter.IsLittleEndian. I'm currently reading up on how IPv6 works. Commented Mar 21, 2021 at 2:50

1 Answer 1

1

The number has to be made into a hex string (12 characters) and then separate the character into groups of 2 characters with a colon between each group. Here is sample code

string input = "281470698521600";
string hex = long.Parse(input).ToString("x");
string[] array = hex.ToCharArray()
    .Select((x, i) => new { chr = x, index = i })
    .GroupBy(x => x.index / 2)
    .Select(x => x.First().chr.ToString() + x.Last().chr.ToString())
    .ToArray();
string IPV6 = string.Join(":", array);

The output would be this : ff:ff:01:00:04:00

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.