We help IT Professionals succeed at work.

C#, populate ArrayList from byte array.

keigva
keigva asked
on
3,721 Views
Last Modified: 2012-06-27
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();
}
Comment
Watch Question

Top Expert 2007

Commented:
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


Top Expert 2007

Commented:
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
Top Expert 2007
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.