2012-04-29 45 views
6

我希望listBox顯示存儲在文件中的ID。這些ID是十六進制值。讀取十六進制文件,轉換爲字符串並加載到列表框

我試圖加載文件並將HEX轉換爲ASCII,因此listBox可以加載它。 該文件還包含一些信息,如項目名稱,項目水平,而這樣...

下面是該文件的一個示例:

00000000h: 50 12 00 00 2F D9 01 00 01 00 00 00 00 00 00 00 ; P.../Ù.......... 
00000010h: 46 65 61 74 68 65 72 20 45 61 72 72 69 6E 67 00 ; Feather Earring. 
00000020h: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ; ................ 
00000030h: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ; ................ 
00000040h: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ; ................ 
00000050h: 00 00 00 00 0A 00 00 00 07 00 02 00 00 00 00 00 ; ................ 
00000060h: 00 00 00 00 00 00 00 00 00 00 00 00 B8 88 00 00 ; ............¸ˆ.. 
00000070h: 00 0C 00 00 00 00 00 00 00 00 00 00 20 00 00 00 ; ............ ... 
00000080h: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ; ................ 
00000090h: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ; ................ 
000000a0h: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ; ................ 
000000b0h: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ; ................ 
000000c0h: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ; ................ 
000000d0h: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ; ................ 
000000e0h: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ; ................ 
000000f0h: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ; ................ 
00000100h: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ; ................ 
00000110h: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ; ................ 
00000120h: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ; ................ 
00000130h: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ; ................ 
00000140h: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ; ................ 
00000150h: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ; ................ 
00000160h: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ; ................ 
00000170h: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ; ................ 
00000180h: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ; ................ 
00000190h: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ; ................ 
000001a0h: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ; ................ 
000001b0h: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ; ................ 
000001c0h: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ; ................ 
000001d0h: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ; ................ 
000001e0h: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ; ................ 
000001f0h: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ; ................ 
00000200h: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ; ................ 
00000210h: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ; ................ 
00000220h: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ; ................ 
00000230h: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ; ................ 
00000240h: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ; ................ 
00000250h: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ; ................ 
00000260h: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ; ................ 
00000270h: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ; ................ 
00000280h: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ; ................ 
00000290h: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ; ................ 
000002a0h: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ; ................ 
000002b0h: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ; ................ 
000002c0h: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ; ................ 
000002d0h: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ; ................ 
000002e0h: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ; ................ 
000002f0h: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ; ................ 
00000300h: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ; ................ 
00000310h: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ; ................ 
00000320h: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ; ................ 
00000330h: 02 00 00 00 00 00 00 00 4A 61 64 65 20 45 61 72 ; ........Jade Ear 
00000340h: 72 69 6E 67 00 00 00 00 00 00 00 00 00 00 00 00 ; ring............ 
00000350h: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ; ................ 
00000360h: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ; ................ 
00000370h: 00 00 00 00 00 00 00 00 00 00 00 00 0A 00 00 00 ; ................ 
00000380h: 07 00 02 00 00 00 00 00 00 00 00 00 00 00 00 00 ; ................ 
00000390h: 00 00 00 00 20 A1 07 00 00 0C 00 00 00 00 00 00 ; .... ¡.......... 
000003a0h: 00 00 00 00 20 00 00 00 00 00 00 00 00 00 00 00 ; .... ........... 
000003b0h: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ; ................ 
000003c0h: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ; ................ 
000003d0h: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ; ................ 
000003e0h: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ; ................ 
000003f0h: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ; ................ 
00000400h: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ; ................ 
00000410h: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ; ................ 
00000420h: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ; ................ 
00000430h: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ; ................ 
00000440h: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ; ................ 
00000450h: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ; ................ 
00000460h: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ; ................ 
00000470h: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ; ................ 
00000480h: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ; ................ 
00000490h: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ; ................ 
000004a0h: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ; ................ 
000004b0h: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ; ................ 
000004c0h: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ; ................ 
000004d0h: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ; ................ 
000004e0h: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ; ................ 
000004f0h: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ; ................ 
00000500h: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ; ................ 
00000510h: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ; ................ 
00000520h: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ; ................ 
00000530h: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ; ................ 
00000540h: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ; ................ 
00000550h: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ; ................ 
00000560h: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ; ................ 
00000570h: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ; ................ 
00000580h: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ; ................ 
00000590h: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ; ................ 
000005a0h: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ; ................ 
000005b0h: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ; ................ 
000005c0h: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ; ................ 
000005d0h: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ; ................ 
000005e0h: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ; ................ 
000005f0h: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ; ................ 
00000600h: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ; ................ 
00000610h: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ; ................ 
00000620h: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ; ................ 
00000630h: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ; ................ 
00000640h: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ; ................ 
00000650h: 00 00 00 00 00 00 00 00 03 00 00 00 00 00 00 00 ; ................ 

忽略「50 12 00 00 2F D9 01 00「,這是文件版本和其他不相關的數據,我想解析到listBox中的是文件版本後的項目ID」01「。

下一個項目ID是「00000330h」,值是「02」。邁向文件的最後,你可以看到有一個ID爲「03」 ......

編輯:

好了,現在我加載該文件,並讀取該項目的部分在它的內部,但我沒有線索如何將ID加載到列表框中... ID彼此分隔807個字節,因此每個807個字節都有一個ID。

這裏是我正在加載該文件的部分

int itemSectionStart = 0x00000000; 
    int itemSectionEnd = 0x002e11e0; 

    FileStream fstream = new FileStream("C:\\Users\\xxx\\Desktop\\xxx\\xxx\\xxx.dec", FileMode.Open); 
    BinaryReader breader = new BinaryReader(fstream); 
    long length = fstream.Length; 

    breader.BaseStream.Position = itemSectionStart; 
    byte[] itemSection = breader.ReadBytes(itemSectionEnd); 
+0

這使我想起了PC的保存遊戲文件,並具有與ELITE學分:) –

+0

的噸哈哈是啊,這是這樣的:P –

+0

不明白你爲什麼要十六進制ASCII轉換?這個十六進制轉儲只是在一些十六進制查看器中對文件進行視覺呈現? –

回答

1

終於完成了我一直在尋找的東西! 這裏是我走上使它工作的方法:(也許不漂亮,但它的作品!)

private string hex2dec(string hexString) 
{ 
    StringBuilder sb = new StringBuilder(); 
    sb.Append(long.Parse(hexString, System.Globalization.NumberStyles.HexNumber)); 
    return sb.ToString(); 
} 

private void button1_Click(object sender, EventArgs e) 
{ 
    int numRecords = 3737; 
    int itemSectionStart = 0x00000008; 
    int itemSectionEnd = 0x002e11ec; 

    using (FileStream str = File.OpenRead("C:\\Users\\xxx\\Desktop\\xxx\\xxx\\xxx.dec")) 
    { 
     BinaryReader breader = new BinaryReader(str); 
     breader.BaseStream.Position = itemSectionStart; 
     byte[] itemSection = breader.ReadBytes(itemSectionEnd); 

     int j = 0; 
     int k = j++; 
     for (int i = 0; i < numRecords; i++) 
     { 
      string id = BitConverter.ToString(itemSection, 808 * k++, 7); 
      string[] strArrayID = id.Split(new char[] { '-' }); 
      string reversedID = strArrayID[6] + strArrayID[5] + strArrayID[4] + strArrayID[3] + strArrayID[2] + strArrayID[1] + strArrayID[0]; 
      listBox1.Items.Add(this.hex2dec(reversedID)); 
     } 
    } 
} 
+0

我不確定我是否理解你的固定方式,將其視爲Hex字符串。你打算在其他地方使用十六進制字符串嗎?爲方便起見,您的文本編輯器以十六進制格式顯示數據,但底層ID數據與十六進制或ASCII無關。如果你的代碼正在工作,那很好,但我認爲你正在做的事情比你真正需要做的多得多。 –

+0

如果它沒有壞,不要修復它大聲笑 它對我來說工作得很好。 –

2

假設你知道如何定位你在文件中想要的ID,你知道它們的寬度(例如,你知道他們是包括4個字節一個Int32),你可以使用C#的BitConverter類,它需要一個字節數組和一個索引,並且給你回到相應的數字類型。如果排序是個問題,你可能需要交換一些東西。

將您的文件讀入一個字節數組,在您關心的每個偏移量處使用BitConverter,將數字轉換爲字符串並加載您的ListView。

int startIndex = 8; 
int recordStride = 128; 
int numRecords = 4; 

using (FileStream str = File.OpenRead("myfile.ext")) { 
    for (int i = 0; i < numRecords; i++) { 
     byte[] data = new byte[4]; 
     str.Seek(startIndex + recordStride * i, SeekOrigin.Begin); 
     str.Read(data, 0, 4); 
     if (NeedToByteSwap) 
      Array.Reverse(data); 

     int id = BitConverter.ToInt32(data, 0); 
     AddToListView(id.ToString()); 
    } 
} 

根據需要填寫或計算您的起始參數。

+0

謝謝!我用你的代碼作爲基礎去做我想做的事。 –

0

聽起來不像數據是十六進制格式 - 聽起來更像是編輯器給你的表示。相反,它似乎更可能是該文件是一個直接的二進制轉儲文本編碼爲ASCII字節流。

在這種情況下,您希望隔離一個表示字符的字節[]範圍(是否有定義記錄,名稱等的明確定義?)。 Feed到 System.Text.ASCIIEncoding(http://msdn.microsoft.com/en-us/library/system.text.asciiencoding.aspx)

其中一個GetString重載將字節範圍轉換爲ASCII文本。 (http://msdn.microsoft.com/en-us/library/744y86tc.aspx)

+0

這些ID彼此相距807個字節,中間還有關於每個項目的其他數據。 這些值爲十六進制,但是它們是「反轉」的,所以文件會顯示「32 DF 14 3C」反轉的十六進制值,其實際上是「3C 14 DF 32」並且將以十進制形式轉換爲「1008000818」。 –

相關問題