簡體   English   中英

如何將主機名轉換為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++='\0';
printf("%s\n",a);
return 0;

當我嘗試在終端中打印時,顯示空白。

代替使用charunsigned 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++='\0';
    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++='\0';
    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');

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM