如何将主机名转换为 DNS 名称?
How to convert hostname to DNS name?
我正在尝试制作将主机名转换为 DNS 名称的程序。
因此,如果我有 www.google.com,我想将其转换为 3www6google3com0
我尝试使用此代码,但它不起作用。谁能告诉我我做错了什么?
int main()
{
unsigned char *a,niz[65536];
unsigned char host[]="www.google.ba";
a=(unsigned char*)&niz[12];
int lock = 0 , i;
strcat((char*)host,".");
for(i = 0 ; i < strlen((char*)host) ; i++)
{
if(host[i]=='.')
{
*a++ = i-lock;
for(;lock<i;lock++)
{
*a++=host[lock];
}
lock++;
}
}
*a++='[=10=]';
printf("%s\n",a);
return 0;
当我尝试在终端中打印时显示空白 space。
而不是 unsigned char
使用 char
。使用 strtok
之类的工具来标记原始字符串。 sprintf
将 int 转换为 c 类型字符串。
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
int main()
{
char *a,niz[65536];
char host[]="www.google.ba";
a=( char*)&niz[20];
strcat(a," ");
char * token = strtok(host,".");
char buffer[20];
while( token != NULL)
{
int len= strlen(token);
sprintf(buffer,"%d",len);
strcat(a,token);
strcat(a,buffer);
token=strtok(NULL,".");
}
*a++='[=10=]';
printf("%s\n",a);
return 0;
}
O/P
www3google6ba2
编辑:
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
int main()
{
char *a,niz[65536];
char host[]="www.google.ba";
a=( char*)&niz[21];
strcat(a," ");
char * token = strtok(host,".");
char buffer[20];
while( token != NULL)
{
int len= strlen(token);
sprintf(buffer,"%d",len);
strcat(a,token);
strcat(a,buffer);
token=strtok(NULL,".");
}
*a++='[=11=]';
int last_len=strlen(a);
a[last_len-1]='0';
printf("%s\n",a);
return 0;
}
O/P
www3google6ba0
编辑:3
这是解决您的问题的提示:
#include<stdio.h>
int main()
{
char c='0';
printf("%d\n",c);
c='3';
printf("%d\n",c);
c='3';
printf("%d\n",c-'0');
}
我正在尝试制作将主机名转换为 DNS 名称的程序。
因此,如果我有 www.google.com,我想将其转换为 3www6google3com0
我尝试使用此代码,但它不起作用。谁能告诉我我做错了什么?
int main()
{
unsigned char *a,niz[65536];
unsigned char host[]="www.google.ba";
a=(unsigned char*)&niz[12];
int lock = 0 , i;
strcat((char*)host,".");
for(i = 0 ; i < strlen((char*)host) ; i++)
{
if(host[i]=='.')
{
*a++ = i-lock;
for(;lock<i;lock++)
{
*a++=host[lock];
}
lock++;
}
}
*a++='[=10=]';
printf("%s\n",a);
return 0;
当我尝试在终端中打印时显示空白 space。
而不是 unsigned char
使用 char
。使用 strtok
之类的工具来标记原始字符串。 sprintf
将 int 转换为 c 类型字符串。
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
int main()
{
char *a,niz[65536];
char host[]="www.google.ba";
a=( char*)&niz[20];
strcat(a," ");
char * token = strtok(host,".");
char buffer[20];
while( token != NULL)
{
int len= strlen(token);
sprintf(buffer,"%d",len);
strcat(a,token);
strcat(a,buffer);
token=strtok(NULL,".");
}
*a++='[=10=]';
printf("%s\n",a);
return 0;
}
O/P www3google6ba2
编辑:
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
int main()
{
char *a,niz[65536];
char host[]="www.google.ba";
a=( char*)&niz[21];
strcat(a," ");
char * token = strtok(host,".");
char buffer[20];
while( token != NULL)
{
int len= strlen(token);
sprintf(buffer,"%d",len);
strcat(a,token);
strcat(a,buffer);
token=strtok(NULL,".");
}
*a++='[=11=]';
int last_len=strlen(a);
a[last_len-1]='0';
printf("%s\n",a);
return 0;
}
O/P www3google6ba0
编辑:3 这是解决您的问题的提示:
#include<stdio.h>
int main()
{
char c='0';
printf("%d\n",c);
c='3';
printf("%d\n",c);
c='3';
printf("%d\n",c-'0');
}