Source code Pub: C # binary output of IPv6 addresses

Source: Internet
Author: User

On the msdn Chinese forum, I encountered a problem where I wrote the relevant code as required by the landlord. I don't have much technical skills.
Download the following example:
Http://files.cnblogs.com/Jason_z/Ipv6ConvertTest.rar

If you are not familiar with the syntax format of IPv6 addresses, read: http://375362she.blog.51cto.com/365362/73629

The IPv6 address provided by the landlord is: fe80: c960: 55bf: bb18: 611c. If you are familiar with IPv6, you will know that this is an IPv6 address in the form of zero compression,

This makes the problem a little troublesome, but fortunately, IPv6 only allows you to use zero compression once. To determine the number of digits of zero:, you can calculate the number of blocks in the compressed address, use 8 Minus this number. Another problem is the numerical type conversion. The obtained IPv6 address should be in a hexadecimal format similar to the preceding format and be converted to a binary format, you should first convert the hexadecimal format to the decimal format, and then convert the decimal format to the binary format:

PS: Other hexadecimal conversions are as follows:
// Convert decimal to binary
Console. writeline (convert. tostring (69, 2 ));
// Convert decimal to octal
Console. writeline (convert. tostring (69, 8 ));
// Convert decimal to hexadecimal
Console. writeline (convert. tostring (69, 16 ));
// Convert binary to decimal
Console. writeline (convert. toint32 ("100111101", 2 ));
// Octal to decimal
Console. writeline (convert. toint32 ("76", 8 ));
// Convert hexadecimal to decimal
Console. writeline (convert. toint32 ("FF", 16 ));

Publish the source code:

Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;
Using system. Text. regularexpressions;

Namespace using 6converttest
{
Class Program
{
Static void main (string [] ARGs)
{
String STR = "fe80: c960: 55bf: bb18: 611c ";

String [] str1 = RegEx. Split (STR, @ "[:] + ");

// The IP address is not compressed to zero.
If (str1.length = 8)
{
For (VAR I = 0; I <str1.length; I ++)
{
Console. Write (convert. tostring (convert. touint32 (str1 [I], 16), 2 ));

If (I! = Str1.length-1)
{
Console. Write (":");
}
}
}

// The IP address is compressed to zero.
Else
{
// Output is not compressed to zero
For (var j = 0; j <str1.length; j ++)
{
STR = Str. Replace (str1 [J], convert. tostring (convert. touint32 (str1 [J], 16), 2 ));
}

Console. writeline (STR );


/* Output is not compressed to zero.
* Int M = 8-str1.length; // calculates the number of compressed zeros.
* String tmp1 = ""; // stores compressed zero strings
* For (var k = 0; k <m; k ++)
*{
* Tmp1 + = "0 :";
*}
* For (var j = 0; j <str1.length; j ++)
*{
STR = Str. Replace (str1 [J], convert. tostring (convert. touint32 (str1 [J], 16), 2 ));
*}
* STR = Str. Replace (":", ":" + tmp1 );
* Console. writeline (STR );
*/
}
Console. readkey ();
}
}
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.