where is the ERROR in my code " Out of bounds Problem"

82 Views Asked by At

i am trying to create a decoding PDU method FOR "GSM" modems (ussd messages) all works fine but i have an error that occues which says"index out of Bounds" ,even if i fix it in one place it occures in other places

i hope you can guide me to the solution of this problem

 public static string PduDecode(byte[] bytes)
    {
        string result = string.Empty;

       
       string[] mymessage = new string[(bytes.Length/2) +1];
      
       


       int index = 0;
       while (index < mymessage.Length - 1)
       {
            mymessage[index]= Convert.ToString(bytes[index], 2).PadLeft(8, '0');
            index++;
       }

       mymessage[index] =string.Empty;

       while (index >= 0)
       {
            if (!string.IsNullOrWhiteSpace(mymessage[index]))
            {
                mymessage[index] = mymessage[index].Substring(0, index+1);
                mymessage[index]=mymessage[index].PadLeft(8, '0');
            }
            index--;
        }
        index = 0;
       while (index < mymessage.Length-1)
       {
           mymessage[index] = Convert.ToInt32(mymessage[index], 2).ToString("X");
            index++;
       }

       result =string.Join("",mymessage);
       result=HexStringToString(result);




       return result;


    }
1

There are 1 best solutions below

0
Derrar Mourad Riad On

i Found the answer for my problem and the method worked perfectly after trying what @CodeCaster suggested and thanks to the remarks of @IvanKhorin the problem was that i was incrementing twice during the loop and then when i was trying to reach for the next element and most of the loops i wasn't incrementing the index Thank you very much