xxd와 같은 hexdump를 만들려고하고 있지만 해결해야 할 몇 가지 차이점이 있습니다. 현재 프로그램은 xxd의 최대 오른쪽 열 vs 16에서 볼 수 있듯이 한 줄당 10자를 처리합니다. 또한 2 옥텟 쌍 대신 칼럼 당 1 옥텟 만 표시합니다.hexdump 출력 대 xxd 출력
는
0000000: 2369 6e63 6c75 6465 203c 7374 6469 6f2e #include <stdio.
내 출력을 xxd
0: 23 69 6E 63 6C 75 64 65 20 3C #include <
편집 :
나는 두 가지를 달성하기 위해 노력하고, 약간의 설명을 추가 할 수 있습니다. 1)이 프로그램에서 을 정확히처럼 출력하고 싶습니다. 이를 위해 32 개의 16 진수 (4x 8x 열)를 출력해야합니다. 2) xxd에서와 같이 행의 4 열에 16 진수를 나열하는 프로그램을 원합니다.
"12"와 같은 소스에서 "10"을 편집하려고 시도했지만 출력에 오류가 발생하는 것으로 보입니다.이 파일은 magic number 인 것으로 보입니다.
자료 :
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <ctype.h>
#define BYTE_OFFSET_INIT 8
#define CHAR_OFFSET_INT 39
#define LINE_LENGTH 50
static void print_e(int e, char *program, char *file)
{
fprintf(stderr, "%s: %s: %s\n", program, file, strerror(e));
}
static void print_line(char *line)
{
int i;
/* sprintf leaves terminators mid-line, so clear them out so we can print the full line */
for (i = BYTE_OFFSET_INIT; i < CHAR_OFFSET_INT; i++)
if (line[i] == '\0')
line[i] = ' ';
printf("%s\n", line);
}
int main(int argc, char *argv[])
{
char line[LINE_LENGTH + 1];
int ch;
int character = 0;
int line_offset = 0;
int byte_offset = BYTE_OFFSET_INIT, char_offset = CHAR_OFFSET_INT;
if (argc != 2) {
fprintf(stderr, "Usage: %s [file]\n", argv[0]);
exit(EXIT_FAILURE);
}
FILE *fp = fopen(argv[1], "rb");
if (!fp) {
print_e(errno, argv[0], argv[1]);
exit(EXIT_FAILURE);
}
printf("Offset Bytes Characters\n");
printf("------ ----------------------------- ----------\n");
while ((ch = fgetc(fp)) != EOF) {
if (character == 0) {
sprintf(line, "%6d ", line_offset);
line_offset += 10;
}
sprintf(line + byte_offset, "%02X ", ch);
sprintf(line + char_offset, "%c", isprint(ch) ? ch : '.');
character++;
char_offset++;
byte_offset += 3;
if (character == 10) {
print_line(line);
character = 0;
char_offset = CHAR_OFFSET_INT;
byte_offset = BYTE_OFFSET_INIT;
}
}
if (ferror(fp)) {
print_e(errno, argv[0], argv[1]);
exit(EXIT_FAILURE);
}
if (character > 0)
print_line(line);
if (fclose(fp) == EOF) {
print_e(errno, argv[0], argv[1]);
exit(EXIT_FAILURE);
}
return 0;
}
있음. 그리고, 당신 문제는 무엇입니까? (힌트 : 각 바이트 값 이후의 "10"과 공백은 모두 프로그램에 있고, 이것이 실제 * 소스라면, 수정해야만합니다 ...) – DevSolar
http : // powerfield-software. com /? p = 25 – paxdiablo
고맙습니다. @paxdiablo – John