keigva
asked on
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();
}
"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();
}
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
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.
return decodedOctet.Substring(0, decodedOctet.Length - 1];
return decodedOctet;
}
Jim
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
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