2012-01-03 119 views
49

我需要一個讀取二進制文件並輸出C/C++源代碼數組(代表文件內容)的腳本/工具。有沒有?腳本/工具將文件轉換爲C/C++源代碼數組


(此問題被刪除了前面。我把這個問題早在,因爲它是有價值的。我正在尋找的正是這種在谷歌,並沒有發現任何東西。當然,這是微不足道的自己,但我的代碼如果我能找到這樣一個簡單的腳本,會節省一些時間,因此它是有價值的

這個問題也有很多沒有太多解釋的迴應。請在評論之前發表評論爲什麼你認爲這是沒有價值或不好的價值

這個問題也引起了很多關於我在問什麼的混淆如果有東西目前還不清楚,請詢問。我真的不知道如何更清楚。查看示例的答案。

另外(在提出問題後),我已經有了幾個答案。我只想把/他們在這裏(再次)鏈接,因爲我認爲這可能是爲別人尋找這很有用)

+2

也許人們明白你想要某種反編譯器或類似的東西。你可以將其改寫爲「讀取二進制文件並輸出初始化爲文件內容的數組的C/C++聲明」或類似的內容。 – 2012-01-03 02:10:39

回答

82

在Debian和其他Linux發行版默認安裝(連同vim)的xxd工具,其中,給予-i選項,可以做你想做什麼:

[email protected]:~/Desktop$ echo Hello World\! > temp 
[email protected]:~/Desktop$ xxd -i temp 
unsigned char temp[] = { 
    0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x57, 0x6f, 0x72, 0x6c, 0x64, 0x21, 
    0x0a 
}; 
unsigned int temp_len = 13; 
+4

好啊!它甚至可以在MacOSX中使用。 – Albert 2012-01-03 05:02:45

+0

正是我在找:)謝謝! – 2013-04-16 12:14:09

+2

將xxd集成到Windows上的Visual Studio 2013解決方案時沒有問題。我用[此源](http://www.opensource.apple.com/source/vim/vim-43/vim/src/xxd/xxd.c?txt) – Spike0xff 2015-12-03 15:23:10

5

一個簡單的工具,可以發現here

#include <stdio.h> 
#include <assert.h> 

int main(int argc, char** argv) { 
    assert(argc == 2); 
    char* fn = argv[1]; 
    FILE* f = fopen(fn, "rb"); 
    printf("char a[] = {\n"); 
    unsigned long n = 0; 
    while(!feof(f)) { 
     unsigned char c; 
     if(fread(&c, 1, 1, f) == 0) break; 
     printf("0x%.2X,", (int)c); 
     ++n; 
     if(n % 10 == 0) printf("\n"); 
    } 
    fclose(f); 
    printf("};\n"); 
} 
+0

在「}」之前的字符數組末尾會有多餘的「,」 – rkosegi 2013-01-04 14:30:32

+0

這不是問題,它使用C++進行編譯。 – sashoalm 2013-08-28 08:08:07

0

這個工具在開發者的命令提示符中編譯C.它產生的輸出到顯示創建的「array_name.c」文件中的內容的終端。請注意,某些終端可能會顯示「\ b」字符​​。

#include <stdio.h> 
    #include <assert.h> 

    int main(int argc, char** argv) { 
    assert(argc == 2); 
    char* fn = argv[1]; 

    // Open file passed by reference 
    FILE* f = fopen(fn, "rb"); 
    // Opens a new file in the programs location 
    FILE* fw = fopen("array_name.c","w"); 

    // Next two lines write the strings to the console and .c file 
    printf("char array_name[] = {\n"); 
    fprintf(fw,"char hex_array[] = {\n"); 

    // Declare long integer for number of columns in the array being made 
    unsigned long n = 0; 

    // Loop until end of file 
    while((!feof(f))){ 
     // Declare character that stores the bytes from hex file 
     unsigned char c; 

     // Ignore failed elements read 
     if(fread(&c, 1, 1, f) == 0) break; 
     // Prints to console and file, "0x%.2X" ensures format for all 
     // read bytes is like "0x00" 
     printf("0x%.2X,", (int)c); 
     fprintf(fw,"0x%.2X,", (int)c); 

     // Increment counter, if 20 columns have been made, begin new line 
     ++n; 
     if(n % 20 == 0){ 
      printf("\n"); 
      fprintf(fw,"\n"); 
     } 
    } 

    // fseek places cursor to overwrite extra "," made from previous loop 
    // this is for the new .c file. Since "\b" is technically a character 
    // to remove the extra "," requires overwriting it. 
    fseek(fw, -1, SEEK_CUR); 

    // "\b" moves cursor back one in the terminal 
    printf("\b};\n"); 
    fprintf(fw,"};\n"); 
    fclose(f); 
    fclose(fw); 
}