이진 파일의 이진 파일 16 진수 문자를 변경하는 방법은 무엇입니까?16 진수 찾기 및 바꾸기
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#define BUFFER_SIZE 4096
int main(void)
{
uint8_t *buffer; // Explicit 8 bit unsigned, but should equal "unsigned char"
FILE *file;
char filename[512] = "test.bin";
// We could also have used buffer[BUFFER_SIZE], but this shows memory alloc
if (NULL == (buffer = malloc(BUFFER_SIZE)))
{
fprintf(stderr, "out of memory\n");
return -1;
}
// Being inside a { }, crlf won't be visible outside, which is good.
char *crlf;
if (NULL != (crlf = strchr(filename, '\n')))
*crlf = 0x0;
if (NULL == (file = fopen(filename, "rb")))
{
fprintf(stderr, "File not found: '%s'\n", filename);
return -1;
}
while(!feof(file) && !ferror(file))
{
size_t i, n;
if (0 == (n = (size_t)fread(buffer, sizeof(uint8_t), BUFFER_SIZE, file)))
if (ferror(file))
fprintf(stderr, "Error reading from %s\n", filename);
// Here, n = 0, so we don't need to break: next i-cycle won't run
for (i = 0; i < n; i++)
{
printf("%02X ", buffer[i]);
if (15 == (i % 16))
printf("\n"); // Every 16th byte, a newline
}
}
fclose(file); // file = NULL; // This ensures file won't be useable after fclose
free(buffer); // buffer = NULL; // This ensures buffer won't be useable after free
printf("\n");
return 0;
}
판독 헥스 = "00 EB 00 00 50 E3 02" 헥스 = "00 EB 01 00 37 E3 02"
16 진수 문자열을 다른 문자열로 바꾸는 문제는 텍스트 문자열 하나를 다른 문자열로 바꾸는 것과 같습니다. 유일한 차이점은 텍스트 패턴 대신 바이너리 패턴을 사용한다는 것입니다. 문제를 해결하려면 먼저 한 텍스트 문자열을 다른 텍스트 문자열로 바꾸는 문제를 해결하십시오. 이것은 strstr, memmove 및 strcpy를 포함합니다. 이진 문자열로 전환하려면 strstr 대신 memmem을 사용하고 strcpy 대신 memcpy를 사용하면됩니다. 참조 http://stackoverflow.com/questions/1992253/a-pure-bytes-version-of-strstr – Brandin
test.bin 8D E2 21 0E 8D E2 E6 39 00 EB 21 0E 8D E2 A0 39 00 EB 00 30 A0 E1 78 00 9F E5 01 2C A0 E3 05 10 A0 E1 73 39 00 EB 00 00 50 E3 02 00 00 1A 00 00 E0 E3 27 DE 8D E2 70 80 BD E8 14 50 94 E2 45 6F 84 E2 0F 00 00 0A 21 0E 8D E2 C2 39 00 EB 14 20 A0 E3 04 10 A0 E1 21E 8D E2 D1 39 00 EB 21E 8D E2 8B 39 00 EB 00 30 –
브랜든에게 감사드립니다. 바이너리 파일에서 직접 16 진수 값을 사용 하시겠습니까? –