C#, populate ArrayList from byte array.

I keep getting this same error message,
"Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index" when I try to populate an arraylist from a char[].

What i want to do is just to switch place of chars in octets f.ex. 21 43 65 87 should becomme 12345678
and if the octets are like 21 43 65 F7 it should becomme 1234567

here is my code:
public string SemiOctetDecode(string rawOctet, bool isDecimal)
{
      //
      // Decodes decimal and none decimal semi octets
      //

      ArrayList decodedOctet = new ArrayList(rawOctet.Length);
      if (isDecimal = true && rawOctet[(rawOctet.Length - 2)] == 'F')
      {
            decodedOctet.Capacity = (rawOctet.Length - 1);
      }
       // Switches places of the two numbers in the octet.
      for(int i = 0; i < (decodedOctet.Capacity); i++)
      {
            if (i % 2 == 0)
            {
                  // Takes second char of each octet and put it in first place in corresponding temp "octet"
                  decodedOctet[i] = rawOctet[(i + 1)];
            }
            else
            {
                  // Takes first char of each octet and put it in second place in corresponding temp "octet"
                  decodedOctet[i] = rawOctet[(i - 1)];
            }
      }
      return decodedOctet.ToString();
}
keigvaAsked:
Who is Participating?
 
JimBrandleyConnect With a Mentor Commented:
I had some problems in the last one. This one is tested.

public string SemiOctetDecode(string rawOctet, bool isDecimal)
{
   //
   // Decodes decimal and none decimal semi octets
   //

   StringBuilder sb = new StringBuilder(rawOctet.Length);
   // Switches places of the two numbers in the octet.
   int i = 0;
   while (i < rawOctet.Length - 1)
   {
      sb.Append(rawOctet[i + 1]);
      sb.Append(rawOctet[i]);
      i += 2;
   }
   if (isDecimal && rawOctet[rawOctet.Length - 2] == 'F')
      return sb.ToString().Substring(0, rawOctet.Length - 1);
   return sb.ToString();
}
0
 
JimBrandleyCommented:
You have problems on both ends. If you did not locate a 'F',
decodedOctet[i] = rawOctet[(i + 1)];  <-- Just went past the end of rawOctet

On the first pass, i == 0, so
decodedOctet[i] = rawOctet[(i - 1)];

tries to use rawOcted[ -1];

I'l put together a fix in a bit.

Jim


0
 
JimBrandleyCommented:
Try this one:
public string SemiOctetDecode(string rawOctet, bool isDecimal)
{
   //
   // Decodes decimal and none decimal semi octets
   //

   string decodedOctet = rawOctet;
   // Switches places of the two numbers in the octet.
   int i = 0;
   while (i < decodedOctet.Length - 1)
   {
      decodedOctet[i] = rawOctet[i + 1];
      decodedOctet[i + 1] = rawOctet[i];
      i += 2;
   }
   if (isDecimal && decodedOctet[decodedOctet.Length - 1] == 'F')
      return decodedOctet.Substring(0, decodedOctet.Length - 1];
   return decodedOctet;
}

Jim
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.