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)를 통해 윈도우 화면 미러링 방법

간단한 cfar 알고리즘에 대해

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

안드로이드(android) 전체 화면 시계 앱(clock app) 예제 코드

python ctypes LoadLibrary로 windows dll 로드 및 함수 호출 예제