blah blah blah is here! blah blah » Close

up0down
link

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

last answered one year ago

1 answers

up0down
link

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:

using System;
using System.Text;

class Test
{
static void Main()
{
byte[] bytes = new byte[] { 0x00, 0x01, 0x01, 0x05, 0xa0,
0xa1, 0x01, 0x01, 0x00, 0xa0,
0xa1, 0xa0, 0xa4, 0x00 };
Encoding enc = Encoding.GetEncoding(1252);
string s = enc.GetString(bytes);
s = s.Replace("\xa0\xa1", "\xaa");
s = s.Replace("\x01\x01", "");
s = s.Replace("\x00", "\xff\xfe\xfd");
byte[] bytes2 = enc.GetBytes(s);

// check it worked
Console.Write("{ ");
foreach(byte b in bytes2)
{
Console.Write("0x{0:x2}, ", b);
}
Console.WriteLine("\b\b }");
Console.ReadKey();
}
}


EDIT

Test code to confirm round-tripping works for all 256 byte values:
using System;
using System.Text;

class Test
{
static void Main()
{
byte[] bytes = new byte[256];
for(int i = 0; i < 256; i++) bytes[i] = (byte)i;
Encoding enc = Encoding.GetEncoding(1252);
string s = enc.GetString(bytes);
byte[] bytes2 = enc.GetBytes(s);

bool isExactMapping = true;
for(int i = 0; i < 256; i++)
{
if (bytes[i] != bytes2[i])
{
isExactMapping = false;
break;
}
}
Console.WriteLine(isExactMapping); // true
Console.ReadKey();
}
}

fmillion
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.)

vulpes
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.

Feedback