我正在Linux上进行套接字编程。客户端正在发送以下给定的十六进制值。我必须在服务器端接收这些值并检查设置了位的8,9,10和11th十六进制值。
我正在使用以下代码,但它正在打印一些奇怪的输出。
请在这件事上给予我帮助..

客户:

void infostate(int sockfd,int confd)
{
char responsedata[]={0x7E,0x11,0x01,0xD0,0x00,0x4E,0x2F,0x0F,0x0F,0x70,0xFF,0xF5,0x25,0xF3,0x0D};
int rc;
rc=send(sockfd,responsedata,sizeof(responsedata),0);
if(rc<0)
{
    perror("error in sending\n");
    close(confd);
    close(sockfd);
    exit(-1);
}
else
printf("data successfully send\n");
}


服务器

void identifycmd(char command[],int acceptsd,int sd)
{
     if(command[11]=='8' && command[12]=='3')
{
    char responsedata[14];
    rc=recv(acceptsd,responsedata,sizeof(responsedata),0);
    if(rc<0)
    {
        perror("error in recieving\n");
        close(acceptsd);
        close(sd);
        exit(-1);
    }
            printf("the response data is\n");
            for(i=0;i<sizeof(responsedata);i++)
            {
                    printf("%x",responsedata[i]);
             }
             printf("\n");


    checkretval(responsedata);
    processinfostate(responsedata);

}


}
void processinfostate(char responsedata[])
{
char  z[8];int i,j;

unsigned char mask[]={128,64,32,16,8,4,2,1};
char x;j=9;
for(i=0;i<8;i++)
{
            x=responsedata[j];
    z[i]=(x & mask[i]);
    printf("%c",z[i]);
        j++

     }

}

最佳答案

试试这个代替你的processinfostate函数

void processinfostate(char responsedata[])
{
 char  z[8];int i, j;
 int indexToCheck[4] = {8, 9, 10, 11};
 unsigned char x;

 for(i=0;i<4;i++)
 {
    x=responsedata[indexToCheck[i]];
    printf(" %x ",x);
    for (j = 0; j < 8; j++)
    {
        z[i] = ((x >> j) & 0x1);
        printf(" %x ",z[i]);
    }
    printf("\n");
 }

}

08-05 02:01