2012-04-29 9 views
6

Quiero que listBox muestre los ID almacenados en un archivo. Los ID están en valor hexadecimal.Lea el archivo hexadecimal, conviértalo en cadena y cárguelo en listBox

Estoy atascado tratando de cargar el archivo y convertir el HEX en ASCII, por lo que el listBox puede cargarlo. El archivo también contiene algo más de información, como el nombre del elemento, nivel de artículo, y tal ...

Aquí está una muestra del archivo:

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 ; ................ 

haga caso de la "50 12 00 00 01 2F D9 00 "al principio, es decir, la versión del archivo y otros datos irrelevantes, lo que quiero analizar en el listBox es el identificador del ítem" 01 "que está justo después de la versión del archivo.

La siguiente ID del artículo está en "00000330h" y el valor es "02". Hacia el final de los archivos se puede ver que hay ID "03" ...

EDIT:

bien, ahora estoy cargando el archivo y la lectura de la sección del artículo dentro de ella, pero yo no tienen ni idea de cómo cargar los ID en el listBox ... Los ID están separados entre sí por 807 bytes, por lo que cada 807 bytes hay un ID.

Aquí es como yo estoy cargando la sección del archivo

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

este modo me recuerda a PC guardar archivos de juego y tener ELITE con toneladas de créditos :) jaja sí –

+0

, es algo así: P –

+0

no entiendo por qué quieres conversión de hexadecimal a ASCII?¿Este volcado hexadecimal es solo una presentación visual del archivo en algún visor hexadecimal? –

Respuesta

1

¡Finalmente logré lo que estaba buscando! Aquí es el enfoque que tomé para hacer que funcione: (Puede que no sea bonita, pero funciona!)

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

No estoy seguro de entender su fijación al tratar esto como una cadena hexadecimal. ¿Pretendes usar una cadena hexagonal en otro lugar? Su editor de texto muestra sus datos en hexadecimal como una conveniencia para usted, pero los datos de identificación subyacentes no tienen nada que ver con hexadecimal o ASCII. Si tu código está funcionando, es genial, pero creo que estás haciendo mucho más trabajo de lo que realmente necesitas hacer. –

+0

si no está roto, no lo arregles lol está funcionando bien para mí. –

2

Asumiendo que sabe cómo localizar los ID que desea en el archivo, y lo sabes su anchura (por ejemplo, sabes que son cada 4 bytes que comprenden un Int32), puede usar la clase BitConverter de C#, que toma una matriz de bytes y un índice y le devuelve el tipo numérico correspondiente. Puede que tenga que cambiar las cosas si la endianidad es un problema.

Lea su archivo en una matriz de bytes, utilice BitConverter en cada desplazamiento que le interese, convierta los números en cadenas y cargue su 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()); 
    } 
} 

Rellene o calcule los parámetros de inicio según sea necesario.

+0

¡Gracias! Usé tu código como base para hacer lo que quería. –

0

No parece tanto que los datos estén en formato hexadecimal: suena más como si fuera la representación que el editor le dio. En cambio, parece más probable que el archivo sea un volcado binario directo con texto codificado como flujo de bytes ASCII.

En cuyo caso, querría aislar un rango de bytes [] que represente los caracteres (¿hay una definición clara de lo que delimita registros, nombres, etc.?). El pienso que a System.Text.ASCIIEncoding (http://msdn.microsoft.com/en-us/library/system.text.asciiencoding.aspx)

Una de las sobrecargas GetString convertirá el intervalo de bytes en ASCII texto. (http://msdn.microsoft.com/en-us/library/744y86tc.aspx)

+0

los ID están separados 807 entre sí y en el medio hay otros datos sobre cada elemento. Los valores están en HEX, pero están "invertidos", por lo que el archivo mostraría el valor hexadecimal invertido "32 DF 14 3C", que realmente es "3C 14 DF 32" y se traduciría a "1008000818" en forma decimal. –