c# - how can i modify a small section of bytes in a memory stream, that was written to using binarywriter -


how edit first 4 bytes in memory stream? imagine "bytes" in following code few 100 bytes long. need write place holder of say, 4 bytes of value 0 , come , update bytes new values.

static memorystream stream = new memorystream(); static binarywriter writer = new binarywriter(stream);  writer.write(bytes); 

how 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);     }     { ms.position = currpos; } } static void showbytearray(byte[] array) {     console.writeline("size: {0}", array.length);     for(int = 0; < 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);     } } 

Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -

java - Digest auth with Spring Security using javaconfig -