【问题标题】:Hex to char conversion and dumping output十六进制到字符的转换和转储输出
【发布时间】:2018-01-31 03:02:36
【问题描述】:

我正在尝试将字符缓冲区的前 8 个字节分配为“00 00 00 00 00 00 00 A0”。我能够验证缓冲区的构造是否正确。每当我尝试使用我的实用程序进行 hexdump 时,我都会得到意外的输出。不确定,如果我在这里遗漏了其他东西。

#include<stdio.h>
#include<string.h>

static void hexdump(const char *src, int count)
{
    int i;  

    if (count == 0)
        return;

    for (i = 0; i < count; ++i) {

        printf("%02x ", src[i]);
        if ((i + 1) % 16 == 0)
            printf("\n");
    }   
    printf("\n");
}

int main() {
   char msg_buff[148];
   memset(msg_buff, 0, 148);
   int a[8] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xA0};
   sprintf(msg_buff, "%02x %02X %02X %02X %02X %02X %02X %02X", a[0], a[1], 
 a[2], a[3], a[4], a[5], a[6], a[7], a[8]);
   printf("packet %s \n", msg_buff); // 00 00 00 00 00 00 00 A0
   hexdump(msg_buff, 148);
}

我的输出

packet 00 00 00 00 00 00 00 A0 
30 30 20 30 30 20 30 30 20 30 30 20 30 30 20 30 
30 20 30 30 20 41 30 00 00 00 00 00 00 00 00 00 
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
00 00 00 00 

预期输出

packet 00 00 00 00 00 00 00 A0 
00 00 00 00 00 00 00 A0 00 00 00 00 00 00 00 00 
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
00 00 00 00 

【问题讨论】:

  • char --> unsigned char 然后unsigned char msg_buff[148]; memset(msg_buff, 0, sizeof msg_buff); unsigned char a[8] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xA0}; memcpy(msg_buff, a, sizeof a); hexdump(msg_buff, sizeof msg_buff);, "%02x " --> "%02X "
  • 这行得通..谢谢
  • @BLUEPIXY:您真的很喜欢将所有答案都放在评论框中。 :)

标签: c casting char hex


【解决方案1】:

首先,您需要从代码中的以下函数中删除a[8],因为a[] 的长度是8,但是您的代码有9 个成员:

sprintf(msg_buff, "%02X %02X %02X %02X %02X %02X %02X %02X", a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7], a[8]);

其次,您需要更改定义的变量类型,您的代码应如下所示:

#include<stdio.h>
#include<string.h>

static void hexdump(const char *src, int count)
{
    int i;

    if (count == 0)
        return;

    for (i = 0; i < count; ++i) {

        printf("%02x ", src[i]);
        if ((i + 1) % 16 == 0)
            printf("\n");
    }
    printf("\n");
}

int main() {

    unsigned char  msg_buff[148];
    memset(msg_buff, 0, 148);
    unsigned char  a[8] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xA0 };
    sprintf(msg_buff, "%02X %02X %02X %02X %02X %02X %02X %02X", a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7]);
    printf("packet %s \n", msg_buff); // 00 00 00 00 00 00 00 A0
    hexdump(msg_buff, 148);

    return 0;
}

【讨论】:

    【解决方案2】:

    改变 hexdump(msg_buff, 148);

    hexdump(a, 8);

    在您的代码中,您正在转储字符串

    【讨论】:

    • 我需要在消息缓冲区中有有效值的十六进制值。
    • 这是什么意思?,你有你放在那里的东西
    猜你喜欢
    • 2015-05-22
    • 2012-01-24
    • 2020-11-07
    • 2013-02-07
    • 2019-07-30
    • 1970-01-01
    • 2021-03-19
    相关资源
    最近更新 更多