[英]How to convert hostname to DNS name?
I am trying to make program that converts hostname to DNS name. 我正在尝试制作将主机名转换为DNS名称的程序。
So if I have www.google.com I want to convert it to 3www6google3com0 因此,如果我拥有www.google.com,我想将其转换为3www6google3com0
I tried with this code but it doesn't work . 我尝试过使用此代码,但是它不起作用。 Can anyone tell me what I am doing wrong?
谁能告诉我我在做什么错?
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++='\0';
printf("%s\n",a);
return 0;
When I try to print it in terminal is shows me blank space. 当我尝试在终端中打印时,显示空白。
Instead of unsigned char
using char
. 代替使用
char
的unsigned char
char
。 Using tools like strtok
to tokenize the original string. 使用
strtok
工具标记原始字符串。 sprintf
to convert int to c-type string. 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++='\0';
printf("%s\n",a);
return 0;
}
O/P www3google6ba2 O / P www3google6ba2
Edit: 编辑:
#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++='\0';
int last_len=strlen(a);
a[last_len-1]='0';
printf("%s\n",a);
return 0;
}
O/P www3google6ba0 O / P www3google6ba0
Edit:3 It is a hint to resolve your issue: 编辑: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');
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.