簡體   English   中英

如何在文件中寫入十六進制數據,以便不寫入其他數據(如 hexeditors)?

[英]How can I write hexadecimal data in the file so that additional data is not written (like hexeditors)?

我想為我的項目制作一個小型十六進制編輯器。

所以我寫了一個這樣的函數(用新代碼替換原始代碼):

int replace(FILE *binaryFile, long offset, unsigned char *replaced, int length) {
    if (binaryFile != NULL) {
        for (int i = 0; i < length; i++) {
            fseek(binaryFile, offset + i, SEEK_SET);
            fwrite(&replaced[i], sizeof(replaced), 1, binaryFile);
        }
        fclose(binaryFile);
        return 1;
    } else return -1;
}

所以我寫了這段代碼來測試這個函數並將它發送到地址0x0:

unsigned char code[] = "\x1E\xFF\x2F\xE1";

我得到了這個十六進制結果:

1e ff 2f e1 00 10 2b 35 ff fe 07 00

但我在 E1 (00 10 2b 35 ff fe 07 00) 之后不需要數據

如何編寫函數以便只存儲發送到函數的數據?

sizeof(replaced)是錯誤的。 replaced是一個unsigned char * ,所以這不是你想要的大小。

您可能需要sizeof(unsigned char)sizeof(*replaced)

目前,你最終寫了八倍。

請注意,您也可以一步編寫:

if (binaryFile != NULL) 
{
    fseek(binaryFile, offset, SEEK_SET);
    fwrite(replaced, sizeof(unsigned char), length, binaryFile);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM