Reputation: 949
I am trying to assign the first 8bytes of a character buffer with "00 00 00 00 00 00 00 A0". I am able to verify the buffer is constructed right. Whenever i try to hexdump using my utility, i get unexpected output. Not sure, if i am missing something else here.
#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);
}
My output
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
Expected output
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
Upvotes: 0
Views: 119
Reputation: 48
Firstly, you need to delete a[8]
from the following function in your code, because the length of a[]
is 8, however your code has 9 members:
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]);
Secondly, you need to change your defined variables type, your code should look like the following:
#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;
}
Upvotes: 1
Reputation: 67638
Change
hexdump(msg_buff, 148);
to
hexdump(a, 8);
In your code you are dumping the string
Upvotes: 0