【问题标题】:sending hexadecimal values in c and check it for bit set在 c 中发送十六进制值并检查它的位设置
【发布时间】:2014-01-03 06:00:07
【问题描述】:

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

客户:

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++

     }

}   

【问题讨论】:

  • 这段代码甚至无法编译。
  • 它会产生什么样的“奇怪输出”?
  • 为什么将数据作为字符发送?您可以将其作为短裤或长期发送并节省带宽。也使数据的使用更容易。
  • 你能不能用一个例子来解释一下这个req“检查8,9,10和第11个十六进制值设置了哪些位”
  • 之前的代码有一些错误,所以我已经编辑了代码。

标签: c linux sockets hex


【解决方案1】:

试试这个,而不是你的 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");
 }

} 

【讨论】:

    猜你喜欢
    • 2016-04-24
    • 2014-11-20
    • 2015-07-25
    • 2019-02-21
    • 1970-01-01
    • 2015-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多