-2

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
    What is this supposed to do anyway? `mymessage[index + 1] =string.Empty` You're addressing the array outside its size there, and for what reason? I also don't get what you're trying to do in those `while` loops. Start by writing down your expectations of that code, then step through it in the debugger. You'll see you ignore return values and not change `index`, so you've got infinite loops. – CodeCaster Dec 25 '20 at 21:20
  • Can you point the line this happens? – Ivan Khorin Dec 25 '20 at 21:20
  • @CodeCaster when decoding PDU message we need to create an 8th byte to recover the previous message ,our entry array has 7 slots while the exiting array should have 8 slots ,i hope my explanation is understandable – Derrar Mourad Riad Dec 25 '20 at 21:25
  • 2
    The index is incremented in the last iteration of the first loop. Just step through your code using F10 and F11 to step into methods. Look at the relevant variables at every step and check they confirm your expectations. – CodeCaster Dec 25 '20 at 21:29
  • @IvanKhorin i changed the color of error line to blue – Derrar Mourad Riad Dec 25 '20 at 21:29
  • Are you sure that this code works well? I mean, after you'll solve your problem, you'll face another ones with infinite loops... – Ivan Khorin Dec 25 '20 at 21:50
  • @IvanKhorin Thank you for the remark i didn't notice it untill now , i am fixing the loops problem while trying with the out of bounds – Derrar Mourad Riad Dec 25 '20 at 22:05
  • One issue is that you're incrementing index based on the size of `bytes`, but your dealing with `mymessage` which is always shorter. – juharr Dec 25 '20 at 22:14
  • @CodeCaster Thank you for this greate advice i am trying it now ,i found other problems in the process and fixed most of them , – Derrar Mourad Riad Dec 25 '20 at 22:33
  • @juharr actually sir the mymessage array is longer then bytes the extra slot can be accessed from bytes.Length+1 – Derrar Mourad Riad Dec 25 '20 at 22:35
  • No you defined it as shorter `string[] mymessage = new string[(bytes.Length/2) +1];` since you divide the length of `bytes` by 2 (unless it's length is 2 or less) – juharr Dec 25 '20 at 23:28
  • @juharr actually sir now that you mention it i didn't check for the length of the byte and i will be ading it now and change the method so it works with no errors – Derrar Mourad Riad Dec 26 '20 at 11:50

1 Answers1

-1

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