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 사용 예


댓글

이 블로그의 인기 게시물

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

간단한 cfar 알고리즘에 대해

Embedded Linux USB Ethernet Gadget 사용하기

쉽게 설명한 파티클 필터(particle filter) 동작 원리와 예제

python winsound를 이용한 윈도우 환경에서 소리 재생 예제