使用C语言编写4个函数,分别实现下列功能 1)将IPv6的二进制地址格式表示形式转换为冒号16进制表示形式;
发布网友
发布时间:2023-07-13 23:34
我来回答
共1个回答
热心网友
时间:2023-09-25 04:26
#include <stdio.h>
#include <string.h>
char FourBinBitStrToHexChar(char *pStar)
{
char HexChar = 0;
char *m = pStar;
while(m<pStar+4)
{
HexChar = HexChar<<2 + *m - '0';
m++;
}
return HexChar>9?(HexChar-10+'A'):(HexChar + '0');
}
int main()
{
char IPv4BinStr[129];
char IPv6HexStr[40];
int i;
char *p;
strcpy(IPv4BinStr, "11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111");
memset(IPv6HexStr, 0 , 40);
printf("IPv4 Binary String is:\n%s\n\n", IPv4BinStr);
for(i=0,p=IPv4BinStr;i<39;i++)
{
if((i%5)!=4)
{
IPv6HexStr[i] = FourBinBitStrToHexChar(p);
p+=4;
}
else
{
IPv6HexStr[i] = ':';
}
}
printf("IPv6 Hex String is:\n%s\n\n", IPv6HexStr);
return 0;
}
追问谢谢啦,我得好好看看,不懂的还要继续请教您哈