Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace byte 10 by 10 10

Tags:

c#

sequence

Hi I want to replace a byte[] where ever there is 10 by 10 10. here is my code. if i have my data as "10 20 10 20 40 50 50 50 50 10 03" i want to replce it by "10 20 10 10 20 40 50 50 50 50 10 10 03" note: first byte is untouched plse follow my comment, my idea is to push the byte array to nxt position and add another 10.

 foreach (var b in command.ToBytes()) 
                {

                   // var c = b;
                    transmitBuffer[count++] = b;  data is formed here
                    addedbuffer[addall++] = b;     duplication is made
                }
                if (addedbuffer[0] == 16 && addedbuffer[1] == 32 || addedbuffer[50] == 16 && addedbuffer[51] == 03)           /
                    {
/condition on which to enter here
                        addedbuffer[0] = 0;  //making 1st and 2nd value as null
                        addedbuffer[1] = 0;
                        for (int i = 0; i < addedbuffer.Length; i++) //till length i will chk 
                        {
                            if (addedbuffer[i] == 10) //replace 10  by 10 10 
                                addedbuffer[i] = 1010; // error,
                        }




        }
like image 440
Anil Gadiyar Avatar asked Jul 19 '26 00:07

Anil Gadiyar


2 Answers

You can't insert into array (you can do it with List<T>), so it looks that you have to create a new array; Linq solution:

  Byte[] source = new Byte[] {
    20, 10, 20, 40, 50, 50, 50, 50, 10, 03
  };

  var result = source
    .SelectMany((item, index) => 
       item == 10 && index != 0 ? new Byte[] { item, item } : new Byte[] { item })
    .ToArray();

However, using List<Byte> (in order just to insert 10's) instead of Byte[] is a better way out:

  List<Byte> list = List<Byte>() {
    20, 10, 20, 40, 50, 50, 50, 50, 10, 03
  };

  // In order not to read inserted 10's we loop backward 
  // i >= 1: 1st byte should be preserved as is even if its == 10
  for (int i = list.Count - 1; i >= 1; --i)
    if (list[i] == 10)
      list.Insert(i + 1, 10);
like image 165
Dmitry Bychenko Avatar answered Jul 20 '26 12:07

Dmitry Bychenko


It helps to think of sequences like arrays (IEnumerable<T> in C#) as things that can be transformed into new sequences. Much like numbers can be transformed when you send them into a function, sequences can be, too.

Consider if I have a function that is defined as Add10If10(Byte b). It might looks like this:

public static Byte Add10If10(Byte b)
{
    if (b == 10)
    {
        return b + 10;
    }

    return b;
}

Numbers which go into this are transformed based on the condition, and come out either 10 larger or the same. The same can be done with sequences, you can take a sequence with some number of elements, and transform it so it has more elements. The result is a new sequence:

public static IEnumerable<Byte> AddAdditional10If10(IEnumerable<Byte> values)
{
    foreach (var b in values)
    {
        if (b == 10)
        { 
            yield return 10;
        }

        yield return b;
    }
}

This function returns an additional 10 for every 10 it encounters. Now that you have the right sequence, you can change how it is stored by changing it to an array:

AddAdditional10If10(addedbuffer).ToArray();
like image 23
codekaizen Avatar answered Jul 20 '26 14:07

codekaizen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!