2016-05-28 2 views
1

comment puis-je éditer les quatre premiers octets dans le flux de mémoire? Imaginez "octets" dans le code suivant est quelques 100 octets de long. J'ai besoin d'écrire un espace réservé de dire, 4 octets de valeur 0 et revenir et mettre à jour ces octets à de nouvelles valeurs.comment puis-je modifier une petite section d'octets dans un flux de mémoire, qui a été écrit à l'aide binarywriter

static MemoryStream stream = new MemoryStream(); 
static BinaryWriter writer = new BinaryWriter(stream); 

writer.Write(bytes); 

Répondre

1

Que diriez-vous de cette solution:

static void UpdateNthLong(MemoryStream ms, long idx, long newValue) 
{ 
    var currPos = ms.Position; 
    try 
    { 
     var offset = sizeof(long) * idx; 
     ms.Position = offset; 
     var bw = new BinaryWriter(ms); 
     bw.Write(newValue); 
    } 
    finally { ms.Position = currPos; } 
} 
static void ShowByteArray(byte[] array) 
{ 
    Console.WriteLine("Size: {0}", array.Length); 
    for(int i = 0; i < array.Length; i++) 
    { 
     Console.WriteLine("{0} => {1}", i, array[i]); 
    } 
} 
static void Main(string[] args) 
{ 
    using (var ms = new MemoryStream()) 
    { 
     var bw = new BinaryWriter(ms); 
     bw.Write(1L); // 0-th 
     bw.Write(2L); // 1-th 
     bw.Write(3L); // 2-th 
     bw.Write(4L); // 3-th 
     var bytes = ms.ToArray(); 

     Console.WriteLine("Before update:"); 
     ShowByteArray(bytes); 
     // Update 0-th 
     UpdateNthLong(ms, 0, 0xFFFFFFFFFFFFFF); 
     // Update 3-th 
     UpdateNthLong(ms, 3, 0xBBBBBBBBBBBBBBB); 

     bytes = ms.ToArray(); 
     Console.WriteLine("After update:"); 
     ShowByteArray(bytes); 
    } 
}