linux 개발 및 디버깅에 유용한 hexdump 로그 c 소스 코드

 로그 메시지만으로 개발 및 디버깅을 하는 경우 메모리 버퍼 내에 어떤 데이터가 들어있는지 눈으로 확인하면 편한 경우가 많다. 이럴 때 사용하면 좋은 예제 코드이다.


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


void hexdump(unsigned char *data, int len)
{
  int i = 0;
  int str_i = 0;
  char *hexdigits = "0123456789abcdef";
  unsigned char str[67] = { 0, };

  if (!data || len <= 0)
  {
  return;
  }

  memset(str, ' ', sizeof(str));

  for (i = 0; i < len; i++)
  {
  str[str_i * 3] = hexdigits[data[i] >> 4];
  str[str_i * 3 + 1] = hexdigits[data[i] & 0xf];
          str[str_i * 3 + 2] = ' ';
  str[50 + str_i] = isprint((int)data[i]) ? (char)data[i] : '.';
          str_i++;

          if (str_i == 16)
  {
               str_i = 0;
  str[48] = ':';
  str[49] = ' ';
  str[66] = 0;
  printf("%s\n", str);
  memset(str, ' ', sizeof(str));
}
  }

  if (str_i > 0)
  {
  str[48] = ':';
  str[49] = ' ';
          str[66] = 0;
  printf("%s\n", str);
  }
}



위 함수 사용결과는 아래와 같다.

hexdump 사용 예


댓글

이 블로그의 인기 게시물

windows에서 간단하게 크롬캐스트(Chromecast)를 통해 윈도우 화면 미러링 방법

Embedded Linux USB Ethernet Gadget 사용하기

간단한 cfar 알고리즘에 대해

python asyncio를 이용한 async socket server client example code

바로 프로젝트 적용 가능한 FIR Filter (low/high/band pass filter )를 c나 python으로 만들기