2010-09-18 21 views
3

J'ai un fichier binaire, où sont quelques valeurs ce qui devrait être changé. Pour être plus précis, à deux parties du dossier, au début, il y a deux valeurs HEXC# Remplacer HEX dans le fichier binaire

66 73 69 6D 35 2E 36 39 

ce qui doit être changé pour

4D 53 57 49 4E 34 2E 31 

Comment pourrais-je faire async, et comme rapide comme possible? Je suis au point où j'ai lu le fichier entier dans un tableau byte [], mais cette classe n'a aucune fonctionnalité de recherche ou de remplacement.

+1

Pourquoi cela doit-il être fait de manière asynchrone? Pouvez-vous simplement exécuter le code dans son propre thread? –

+0

Pourquoi voulez-vous modifier l'enregistrement de démarrage de la disquette? Êtes-vous un auteur de virus ou un auteur d'anti-virus? :-) –

+0

Vous pouvez trouver cela étrange, mais c'est un projet Symbian: D En fait, je veux convertir les images FSIM UDA en images MSWIN modifiables, et vice versa – fonix232

Répondre

4

Voici une méthode que j'ai écrite que vous pouvez utiliser pour trouver où dans votre byte[] les octets sont que vous essayez de trouver.

/// <summary> 
/// Searches the current array for a specified subarray and returns the index 
/// of the first occurrence, or -1 if not found. 
/// </summary> 
/// <param name="sourceArray">Array in which to search for the 
/// subarray.</param> 
/// <param name="findWhat">Subarray to search for.</param> 
/// <param name="startIndex">Index in <paramref name="sourceArray"/> at which 
/// to start searching.</param> 
/// <param name="sourceLength">Maximum length of the source array to search. 
/// The greatest index that can be returned is this minus the length of 
/// <paramref name="findWhat"/>.</param> 
public static int IndexOfSubarray<T>(this T[] sourceArray, T[] findWhat, 
     int startIndex, int sourceLength) where T : IEquatable<T> 
{ 
    if (sourceArray == null) 
     throw new ArgumentNullException("sourceArray"); 
    if (findWhat == null) 
     throw new ArgumentNullException("findWhat"); 
    if (startIndex < 0 || startIndex > sourceArray.Length) 
     throw new ArgumentOutOfRangeException(); 
    var maxIndex = sourceLength - findWhat.Length; 
    for (int i = startIndex; i <= maxIndex; i++) 
    { 
     if (sourceArray.SubarrayEquals(i, findWhat, 0, findWhat.Length)) 
      return i; 
    } 
    return -1; 
} 

/// <summary>Determines whether the two arrays contain the same content in the 
/// specified location.</summary> 
public static bool SubarrayEquals<T>(this T[] sourceArray, 
     int sourceStartIndex, T[] otherArray, int otherStartIndex, int length) 
     where T : IEquatable<T> 
{ 
    if (sourceArray == null) 
     throw new ArgumentNullException("sourceArray"); 
    if (otherArray == null) 
     throw new ArgumentNullException("otherArray"); 
    if (sourceStartIndex < 0 || length < 0 || otherStartIndex < 0 || 
     sourceStartIndex + length > sourceArray.Length || 
     otherStartIndex + length > otherArray.Length) 
     throw new ArgumentOutOfRangeException(); 

    for (int i = 0; i < length; i++) 
    { 
     if (!sourceArray[sourceStartIndex + i] 
      .Equals(otherArray[otherStartIndex + i])) 
      return false; 
    } 
    return true; 
} 
+0

Merci beaucoup, va essayer dès que possible! – fonix232

+0

Malheureusement ne fonctionne pas, même si je crée une nouvelle classe pour cela, il ne trouvera pas les fonctions SubarrayEquals: S – fonix232

+0

@ fonix232: C'est dans mon code. Êtes-vous sûr de l'avoir copié? – Timwi