Is it possible to accomplish something like this?
byte[] bytes = new byte[] { 0x00, 0x01, 0x01, 0x05, 0xa0,
0xa1, 0x01, 0x01, 0x00, 0xa0,
0xa1, 0xa0, 0xa4, 0x00 };
// We now have an array of bytes.
// We want to replace all instances of "0xa0, 0xa1" to "0xaa". So the result would be:
//
// { 0x00, 0x01, 0x01, 0x05, 0xaa, 0x01, 0x01, 0x00, 0xaa, 0xa0, 0xa4, 0x00 };
// We also would like to remove all instance of a double 0x01. So after that we'll have
//
// { 0x00, 0x05, 0xaa, 0x00, 0xaa, 0xa0, 0xa4, 0x00 };
// Finally we want to replace all 0x00's with 0xff, 0xfe, 0xfd. So:
//
// { 0xff, 0xfe, 0xfd, 0x05, 0xaa, 0xff, 0xfe, 0xfd, 0xaa, 0xa0, 0xa4, 0xff, 0xfe, 0xfd };Obviously some of this is roundabout, but this isn't actual data, it's just a sample to illustrate my desired results at various steps in the array manipulation.
I'm probably missing something very elementary, but are these kind of replaces even possible in arrays of individual items like this?
Thanks
fm

1 answers
Well, the problem with arrays is that once you've created them you can't change their length and so simple replacement of elements won't work here.
What I'd do is to use a single byte encoding (such as Windows 1252), to convert the byte array to a string, use string.Replace to make the changes you want and then convert the resulting string back to a (new) byte array.
Here's some code:
EDIT
Test code to confirm round-tripping works for all 256 byte values:
answered one year ago by:
17279
490
Hey, that works exactly like I expected... Can I assume that codepage 1252 is bsaically a one-byte codepage wherein the byte-to-character ratio is exactly 1:1 for every byte in the 256 possible bytes? (The problem of course with ASCII is that only 0-127 maps, the rest are dropped.)
17279
Yes, you can assume that. To make absolutely sure, I tested all 256 characters (including the 5 unused ones) and, on calling GetBytes, they mapped back to exactly the same byte values that they originated from. I've edited my reply to include the test code.