Answered by:
Identificar una secuencia de numeros en un arreglo

Question
-
Hola gente, queria saber como pintar partes de un vector. La cosa es asi tengo un vector de tantas dimensiones y lo que necesito es que si lee dentro de ese vector 0 1 0 31 32 esos valores los pinte de un color cualquier y el resto los deje sin colorear o de otro color distinto. El problema es que dentro del vector esa secuencia de valores se repite varias veces en diferentes partes y como el vector es muy grande al pintarlos es mas fácil ubicar.
- Moved by Jason Dot Wang Wednesday, July 3, 2013 5:41 AM This thread is not written by English
Thursday, June 27, 2013 3:15 AM
Answers
-
It was a typo.... please try it now.
class Program { private static readonly byte[] SpecialSequence = new byte[] { 0, 1, 0, 31, 32 }; private static void Main(string[] args) { int fileLength; byte[] fileContent; using (var fileStream = new FileStream(@"C:\Users\frann\Desktop\FICHEROS\P1.pgm", FileMode.Open, FileAccess.Read)) { fileLength = (int)fileStream.Length; fileContent = new byte[fileLength]; fileStream.Read(fileContent, 0, fileLength); } Console.WriteLine(fileLength); for (int p = 0; p < fileLength; ) { if (DetectSequence(fileContent, p)) { Console.ForegroundColor = ConsoleColor.Red; foreach (byte specialByte in SpecialSequence) { Console.Write(@"{0} ", specialByte); } Console.ForegroundColor = ConsoleColor.White; p += SpecialSequence.Length; continue; } int byteRead = fileContent[p]; Console.Write(@"{0} ", byteRead); p++; } // Wait for user input. Console.ReadKey(); } private static bool DetectSequence(byte[] fileContent, int position) { if (position + SpecialSequence.Length > fileContent.Length) return false; for (int i = 0; i < SpecialSequence.Length; i++) { if (fileContent[position + i] != SpecialSequence[i]) return false; } return true; } }
It can be a little more optimized, but this is more clear.
- Proposed as answer by FernandoRocha Thursday, June 27, 2013 4:42 PM
- Edited by FernandoRocha Thursday, June 27, 2013 4:43 PM typo
- Marked as answer by Just Karl Friday, September 27, 2013 10:18 PM
Thursday, June 27, 2013 4:42 PM
All replies
-
Could you please provide a little code sample?
I can't understand what do you mean by "painting" values of that vector... what data type is that vector? How do you paint numbers of that vector?
Thursday, June 27, 2013 4:13 AM -
static void Main(string[] args)
{
FileStream Archivo = new FileStream(@"C:\Users\frann\Desktop\FICHEROS\P1.pgm", FileMode.Open, FileAccess.ReadWrite);
Archivo.Position = 0;
Console.WriteLine(Archivo.Length);
int leer = 0;
int[] arreglo = new int[Archivo.Length];
for (int i = 0; i < Archivo.Length; i++)
{
arreglo[i] = Archivo.ReadByte();
}
for (int p = 0; p < Archivo.Length; p++)
{
leer = arreglo[p];
if (leer == 0)
{ Console.ForegroundColor = ConsoleColor.White; }
}
for (int j = 0; j < Archivo.Length; j++)
{
Console.Write("{0} ", arreglo[j]);
}Te paso a explicar son bytes que lo paso a un vector por que creo que de esa manera es mejor, lo que necesito es que dentro de ese vector en este caso se llama arreglo identificar 0 1 0 31 32 y cuando encuentre esa secuencia de numeros haga algo yo tenia la idea de pintarlos o que baje de linea cuando encuentre esa secuencia.
Thursday, June 27, 2013 4:19 AM -
This works, please try it:
class Program { private static readonly byte[] SpecialSequence = new byte[] { 0, 1, 0, 31, 32 }; private static void Main(string[] args) { int fileLength; byte[] fileContent; using (var fileStream = new FileStream(@"C:\Users\frann\Desktop\FICHEROS\P1.pgm", FileMode.Open, FileAccess.Read)) { fileLength = (int)fileStream.Length; fileContent = new byte[fileLength]; fileStream.Read(fileContent, 0, fileLength); } Console.WriteLine(fileLength); for (int p = 0; p < fileLength; ) { if (DetectSequence(fileContent, p)) { Console.ForegroundColor = ConsoleColor.Red; foreach (byte specialByte in SpecialSequence) { Console.Write(@"{0} ", specialByte); } Console.ForegroundColor = ConsoleColor.White; p += SpecialSequence.Length; continue; } int byteRead = fileContent[p]; Console.Write(@"{0} ", byteRead); p++; } // Wait for user input. Console.ReadKey(); } private static bool DetectSequence1(IList<byte> fileContent, int position) { if (position + SpecialSequence.Length > fileContent.Count) return false; for (int i = 0; i < SpecialSequence.Length; i++) { if (fileContent[position + i] != SpecialSequence[i]) return false; } return true; } }
It can be a little more optimized, but this is more clear.
- Edited by FernandoRocha Thursday, June 27, 2013 12:39 PM note
- Proposed as answer by FernandoRocha Thursday, June 27, 2013 12:40 PM
Thursday, June 27, 2013 12:29 PM -
No puedo probarlo me dice esto: "Error 1 El nombre 'DetectSequence' no existe en el contexto actual " 27 21 es el lugar falta algun using? perdon que sea tan molesto
Thursday, June 27, 2013 4:19 PM -
It was a typo.... please try it now.
class Program { private static readonly byte[] SpecialSequence = new byte[] { 0, 1, 0, 31, 32 }; private static void Main(string[] args) { int fileLength; byte[] fileContent; using (var fileStream = new FileStream(@"C:\Users\frann\Desktop\FICHEROS\P1.pgm", FileMode.Open, FileAccess.Read)) { fileLength = (int)fileStream.Length; fileContent = new byte[fileLength]; fileStream.Read(fileContent, 0, fileLength); } Console.WriteLine(fileLength); for (int p = 0; p < fileLength; ) { if (DetectSequence(fileContent, p)) { Console.ForegroundColor = ConsoleColor.Red; foreach (byte specialByte in SpecialSequence) { Console.Write(@"{0} ", specialByte); } Console.ForegroundColor = ConsoleColor.White; p += SpecialSequence.Length; continue; } int byteRead = fileContent[p]; Console.Write(@"{0} ", byteRead); p++; } // Wait for user input. Console.ReadKey(); } private static bool DetectSequence(byte[] fileContent, int position) { if (position + SpecialSequence.Length > fileContent.Length) return false; for (int i = 0; i < SpecialSequence.Length; i++) { if (fileContent[position + i] != SpecialSequence[i]) return false; } return true; } }
It can be a little more optimized, but this is more clear.
- Proposed as answer by FernandoRocha Thursday, June 27, 2013 4:42 PM
- Edited by FernandoRocha Thursday, June 27, 2013 4:43 PM typo
- Marked as answer by Just Karl Friday, September 27, 2013 10:18 PM
Thursday, June 27, 2013 4:42 PM -
Gracias Gracias funciona a la perfección :). Si necesitas ayuda en algo no duden en consultarme.Thursday, June 27, 2013 4:51 PM
-
I'm happy to hear that. :)
Please mark my reply as Answer.
Please notice that you can ask question in Spanish at the Spanish forum version.
Best regards,
Fernando Rocha
- Edited by FernandoRocha Thursday, June 27, 2013 10:24 PM notice
Thursday, June 27, 2013 10:04 PM