查找并替换HEX字符串

时间:2012-05-10 18:37:26

标签: c# hex replace

好的,我在这一点上陷入困​​境......我想比较两个文件并将字符串从一个文件复制(替换)到另一个文件。

示例文件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.....蕷;....

我想要做的是将File1与File2进行比较,以查看项目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”相应地是Central Valley和Hill Valley的ID。

2 个答案:

答案 0 :(得分:1)

对我而言,目前尚不清楚为什么要在Hex中执行此操作。看起来你可以把它作为文本来处理。

我的想法是你应该使用正则表达式查找空格分隔的单词并备份几个字符以获取ID字符(如果需要,可以将其转换为十六进制)。

答案 1 :(得分:0)

不是将项目加载到列表框中,而是将项目加载到两个Dictionary对象中(其中整数是项目的ID,字符串是项目的描述。每个文件加载一个字典。然后比较字典中的项目,对一个字典进行任何修改,然后将其保存回文件。