2012-05-10 252 views
0

好的,我被困在這一點...我想比較兩個文件並將字符串從一個文件複製(替換)到另一個文件。查找並替換HEX字符串

示例文件1:

01cd74a8h: 65 00 00 00 54 45 53 54 00 00 00 00 00 00 00 00 ; e...TEST........ 
01cd74b8h: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ; ................ 
01cd74c8h: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ; ................ 
01cd74d8h: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ; ................ 
01cd74e8h: 00 00 00 00 00 00 20 C5 00 00 00 00 00 00 20 C5 ; ...... ?..... ? 
01cd74f8h: 00 00 20 45 00 00 00 00 00 00 20 45 01 00 00 00 ; .. E...... E.... 
01cd7508h: 00 00 00 00 00 00 00 00 C9 00 00 00 54 61 6D 20 ; ........?..Tam 
01cd7518h: 54 61 20 51 75 61 6E 00 00 00 00 00 00 00 00 00 ; Ta Quan......... 
01cd7528h: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ; ................ 
01cd7538h: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ; ................ 
01cd7548h: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 20 45 ; .............. E 
01cd7558h: 00 00 00 00 00 00 20 C5 00 00 F0 45 00 00 00 00 ; ...... ?.餎.... 
01cd7568h: 00 00 20 45 01 00 00 00 00 00 00 00 00 00 00 00 ; .. E............ 
01cd7578h: 2D 01 00 00 4C 69 65 75 20 43 68 69 6E 68 20 51 ; -...Lieu Chinh Q 
01cd7588h: 75 61 6E 00 00 00 00 00 00 00 00 00 00 00 00 00 ; uan............. 
01cd7598h: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ; ................ 
01cd75a8h: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ; ................ 
01cd75b8h: 00 00 00 00 00 00 F0 C5 00 00 00 00 00 00 20 C5 ; ......鶓...... ? 
01cd75c8h: 00 00 20 C5 00 00 00 00 00 00 20 45 01 00 00 00 ; .. ?..... E.... 
01cd75d8h: 00 00 00 00 00 00 00 00 91 01 00 00 56 6F 20 54 ; ........?..Vo T 
01cd75e8h: 68 69 65 6E 20 43 61 63 20 31 00 00 00 00 00 00 ; hien Cac 1...... 
01cd75f8h: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ; ................ 
01cd7608h: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ; ................ 
01cd7618h: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 48 C4 ; ..............H? 
01cd7628h: 00 00 00 00 00 00 48 C4 00 00 48 44 00 00 00 00 ; ......H?.HD.... 
01cd7638h: 00 00 48 44 00 00 00 00 10 CA 9A 3B 00 00 00 00 ; ..HD.....蕷;.... 

示例文件2:

01b954a8h: 65 00 00 00 43 65 6E 74 72 61 6C 20 56 61 6C 6C ; e...Central Vall 
01b954b8h: 65 79 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ; ey.............. 
01b954c8h: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ; ................ 
01b954d8h: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ; ................ 
01b954e8h: 00 00 00 00 00 00 20 C5 00 00 00 00 00 00 20 C5 ; ...... ?..... ? 
01b954f8h: 00 00 20 45 00 00 00 00 00 00 20 45 01 00 00 00 ; .. E...... E.... 
01b95508h: 00 00 00 00 00 00 00 00 C9 00 00 00 48 69 6C 6C ; ........?..Hill 
01b95518h: 20 56 61 6C 6C 65 79 00 00 00 00 00 00 00 00 00 ; Valley......... 
01b95528h: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ; ................ 
01b95538h: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ; ................ 
01b95548h: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 20 45 ; .............. E 
01b95558h: 00 00 00 00 00 00 20 C5 00 00 F0 45 00 00 00 00 ; ...... ?.餎.... 
01b95568h: 00 00 20 45 01 00 00 00 00 00 00 00 00 00 00 00 ; .. E............ 
01b95578h: 2D 01 00 00 52 69 76 65 72 73 69 64 65 20 50 6C ; -...Riverside Pl 
01b95588h: 61 69 6E 73 00 00 00 00 00 00 00 00 00 00 00 00 ; ains............ 
01b95598h: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ; ................ 
01b955a8h: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ; ................ 
01b955b8h: 00 00 00 00 00 00 F0 C5 00 00 00 00 00 00 20 C5 ; ......鶓...... ? 
01b955c8h: 00 00 20 C5 00 00 00 00 00 00 20 45 01 00 00 00 ; .. ?..... E.... 
01b955d8h: 00 00 00 00 00 00 00 00 91 01 00 00 46 6F 72 62 ; ........?..Forb 
01b955e8h: 69 64 64 65 6E 20 54 6F 77 65 72 20 31 46 00 00 ; idden Tower 1F.. 
01b955f8h: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ; ................ 
01b95608h: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ; ................ 
01b95618h: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 48 C4 ; ..............H? 
01b95628h: 00 00 00 00 00 00 48 C4 00 00 48 44 00 00 00 00 ; ......H?.HD.... 
01b95638h: 00 00 48 44 00 00 00 00 10 CA 9A 3B 00 00 00 00 ; ..HD.....蕷;.... 

我想要做的就是比較文件1到文件2,看是否有物品ID的任何比賽,並僅當文件存在時,纔將File2的信息複製到File1。

項目ID從項目名稱後面開始4個字節。

01b954a8h: 65 00 00 00 43 65 6E 74 72 61 6C 20 56 61 6C 6C ; e...Central Vall 
    01b954b8h: 65 79 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ; ey.............. 

    01b95508h: 00 00 00 00 00 00 00 00 C9 00 00 00 48 69 6C 6C ; ........?..Hill 
    01b95518h: 20 56 61 6C 6C 65 79 00 00 00 00 00 00 00 00 00 ; Valley......... 

在這裏您可以看到「65 00」和「C9 00」是中央谷和山谷的ID。

+0

我試圖以一種非常醜陋的方式做到這一點... 加載項目ID和名稱到列表框,並進行比較和替換但它消耗了大量的內存和性能差。 –

回答

1

對我來說目前還不清楚你爲什麼要在Hex中做到這一點。看起來你可以把它作爲文本來處理。

我的想法是,您應該考慮使用正則表達式來查找空格分隔的單詞並備份一些字符以獲取ID字符(如果需要,可以將其轉換爲十六進制)。

+0

可能並非necesarily必須詛咒它cuold是作爲一個字節數組,但文字不讀取空字節「00」。 –

+0

@AlanAlvarez使用字節數組會沒問題,而且可能會比字符串,但沒有理由不使用字符串,如果你想。將數據加載到內存中時,只需將它們解碼爲一個字符串,然後在將它們寫回時進行編碼。當你寫回來時,你必須填充數據,當然,使用空字符。 –

0

而是裝載的物品放入一個列表框,加載的項目分爲兩個字典對象(其中整數是該項目的ID和字符串是該項目的描述。加載每個文件一個字典,然後比較的項目字典,做任何修改,你需要對一個字典,然後將其保存迴文件。