Ygdrasil
이건가? 저건가?
Tags
Guestbook
Admin
Logout
Notice
읽고싶은 책 / 갖고싶은 음반 목록
zookeeper
Search
Category
분류 전체보기
(215)
육아
(1)
Music
(23)
Classical
(11)
Swing
(0)
Others
(12)
English
(27)
NBC Nightly News
(6)
NBC Meet the Press
(4)
Computing
(87)
vim tips
(12)
Books
(8)
아니메, 드라마
(18)
잡동사니
(25)
여행
(9)
사진기
(3)
Australia
(5)
English Writings
(0)
메모
(6)
Swing
(0)
Links
Total :
Today :
Yesterday :
티스토리 가입하기!
hex dump
Computing
2013. 8. 2. 12:21
#include <stdint.h> #include <stdio.h> #include <iostream> #include <assert.h> static void dumpHex(const void *aBuffer, const size_t aBufferSize, size_t aWidth) { char sHexBuffer[6] = {0}; size_t sLineSize; size_t sLineLength; /* the number of bytes printed in a line */ char *sLine = NULL; size_t sPos = 0; size_t i; const uint8_t *sBuffer = (const uint8_t *)aBuffer; const size_t sAddrAreaSize = 6; /* address column (e.g. FFFF ) */ const size_t sColWidth = 4; /* the number of bytes that consists a column (FF FF FF FF FF FF FF FF ) */ aWidth = ((aWidth + (sColWidth - 1)) / sColWidth) * sColWidth; const size_t sHexAreaSize = (aWidth * 3) + /* 3 chars required to display a byte (FF ) - including trailing space */ (aWidth / sColWidth); /* to distinguish a column by inserting additional space */ const size_t sCharAreaStartPos = sAddrAreaSize + sHexAreaSize; sLineSize = sAddrAreaSize + sHexAreaSize + aWidth + 1; /* Null terminator */ sLine = (char *)malloc(sLineSize); assert(sLine != NULL); *(sLine + sLineSize - 1) = '\0'; while (sPos < aBufferSize) { memset(sLine, ' ', sLineSize - 1); sLineLength = ((aBufferSize - sPos) > aWidth) ? aWidth : (aBufferSize - sPos); /* Address */ snprintf(sHexBuffer, sizeof(sHexBuffer), "%04X:", (uint16_t)(sPos & 0xFFFF)); memcpy(sLine, sHexBuffer, 5); /* Hex part */ for (i = 0; i < sLineLength; i++) { snprintf(sHexBuffer, sizeof(sHexBuffer), "%02X", *(sBuffer + sPos + i)); memcpy(sLine + sAddrAreaSize + (i * 3) + (i / sColWidth), sHexBuffer, 2); } /* Character part */ for (i = 0; i < sLineLength; i++) { uint8_t sByte = *(sBuffer + sPos + i); *(sLine + sCharAreaStartPos + i) = (sByte < 127 && sByte >= 0x20) ? (char)sByte : '.'; } sPos += aWidth; std::cout << sLine << "\n"; } free(sLine); fflush(stdout); } int32_t main(void) { uint8_t sBuffer[1063]; size_t i; for (i = 0; i < sizeof(sBuffer); i++) { sBuffer[i] = (uint8_t)(i & 0xff); } dumpHex(sBuffer, sizeof(sBuffer), 18); return 0; }
공유하기
게시글 관리
Ygdrasil
저작자표시
비영리
변경금지
:
티스토리툴바