[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

C#, populate ArrayList from byte array.

Posted on 2007-10-02
3
Medium Priority
?
3,681 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();
}
0
Comment
Question by:keigva
  • 3
3 Comments
 
LVL 22

Expert Comment

by:JimBrandley
ID: 20002577
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
 
LVL 22

Expert Comment

by:JimBrandley
ID: 20002900
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
 
LVL 22

Accepted Solution

by:
JimBrandley earned 1000 total points
ID: 20003696
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

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses
Course of the Month18 days, 17 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question