Question: #include #include using namespace std; void text 2 bin _ IPv 6 ( const char * a , unsigned char * v ) { /

#include
#include
using namespace std;
void text2bin_IPv6(const char* a, unsigned char* v)
{
// a[] is the text format representation of the IPv6 address.
// v[] is the binary IPv6 address to be produced by the conversion function.
// v[0] is the leftmost byte of the 128-bit IPv6 address,
// bit-7 of v[0] is the leftmost (1st) bit of the IPv6 address.
for (int k =0; k <16; k++)
v[k]=0;
// Your codes. You may define additional supporting function where
appropriate.
// This exercise is intended to train your organization skills.
// Length of this function is longer than the previous tutorials.
// Expected number of statements is about 45 to 50.
}
//--------------------------------------- functions given to students
char toHex(unsigned d)
{
if (d >=10)
return d -10+'a';
return d +'0';
}
void printIPv6address_hex(const unsigned char* v)
{
cout << "IPv6 address (hex)";
for (int i =0; i <16; i++)
{
cout << toHex(v[i]/16)<< toHex(v[i]%16);
if (i %2 && i <14)
cout <<':';
}
cout << endl;
}
void printIPv6address_bin(const unsigned char* v)
{
cout << "IPv6 address (bin)";
for (int i =0; i <16; i++)
{
cout << bitset<8>(v[i]);
if (i %2 && i <14)
cout <<"-";
}
cout << endl;
}
void test(const char* a)
{
cout <<"-----------------------------------------------------------
";
cout << "IPv6 address (text)"<< a << endl;
unsigned char v[16]; //16 bytes memory to store the 128-bit IPv6 address
text2bin_IPv6(a, v);
printIPv6address_hex(v);
printIPv6address_bin(v);
}
int main()
{
// Test data : IPv6 addresses in text format
char a1[]="::";
char a2[]="::1";
char a3[]="1234:abc:0:78:ff00:0:30:5";
char a4[]="2001:db8:a3::200:8";
char a5[]="2001:db8::";
char a6[]="::101:7000:3:0:4";
test(a1);
test(a2);
test(a3);
test(a4);
test(a5);
test(a6);
system("pause");
return 0;
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!