Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8690501
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T23:54:57+00:00 2026-06-12T23:54:57+00:00

I am working on a serial commnuication protocol. In this protocol, the packt consists

  • 0

I am working on a serial commnuication protocol. In this protocol, the packt consists of bytes, and it should start and end with 0x7E.

The protocol indicates that any byte which is 0x7E or 0x7D should be escaped to 0x7D 0x5E or 0x7d 0x5D accordingly.

Here is a sample packet:

7E 40 09 00 BE EF 05 7E 06 01 02 03 04 05 7E

As you can see, byte[7] in this packet should be replaced by 2 bytes 0x7D 0x5E. To this I wrote the method below but it fails to do this task:

private List<byte> Finalize(List<byte> packet)
{
    int u = 1; //keep track of end of pack
    //because if escape characters are present
    //end bit will be shifted to right

    //Walk through list and fix escape bytes
    for (int i = 1; i < packet.Count; i++)
    {
        if (packet[i] == 0x7D)
        {
            packet[u] = 0x7D;
            packet[u + 1] = 0x5D;
            u += 2;
        }
        else if (packet[i] == 0x7E) //Sync Byte
        {
            packet[u] = 0x7D;
            packet[u + 1] = 0x5E;
            u += 2;
        }
        else
        {
           packet[u] = packet[i]; //Exception here!!
           u++;
        }
    }

    return packet;
}

The method throws an ArgumentOutOfRangeException. Can someone tell me what can be the problem!? I guess I implemented a correct logic but it seems I didn’t handle indexes properly!

The loop is starting at index 1, because its the start byte and it shouldnt be changed. it also shouldn’t change the last byte in the list which is 0x7E, the end byte!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T23:54:58+00:00Added an answer on June 12, 2026 at 11:54 pm

    First of all you are loosing the data when you overwrite your byte next to bytes with values 0x7D and 0x7E. Second, you are changing u faster than i which produces the Exception mentioned. When you escape your bytes, you add 2 to u but in that iteration of the for loop, i is only incremented. Third, the better approach for this would definitely be while loop.

    To handle the first problem you should add more elements to the list. Insert would do the trick instead of shifting everything manually to the right. The second problem you can solve by using only one counter. Check the code:

            private List<byte> Finalize(List<byte> packet)
            {
                int i = 1;
                while (i < packet.Count-1)
                {
                    if (packet[i] == 0x7D)
                    {
                        packet[i] = 0x7D;
                        packet.Insert(i+1, 0x5D);
                        i += 2;
                    }
                    else if (packet[i] == 0x7E) //Sync Byte
                    {
                        packet[i] = 0x7D;
                        packet.Insert(i+1, 0x5E);
                        i += 2;
                    }
                    else 
                        i++;
                }
                return packet;
           }
    

    The other solution would be creating new list within the method and adding appropriate values to it.
    while (i < packet.Count-1) is because I think that you don’t want to escape the ending byte. Have in mind that the last byte in your list is packet[packet.Count-1] not packet[packet.Count]

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working with a database that has a bunch of serial numbers that are
I am working with serial communication and I'm wondering whether I should keep the
I'm working on an observer that needs to add (a) serial key(s) to each
I am working with a piece of hardware that I'm communicating with VIA serial
I am working on a parser for a serial data protocol. I have an
I am working on a program that reads in from a serial port, then
I'm working on serial communication with a multimeter VA18B that uses PC Link data
I'm working on an App that talks to a serial port, and on my
I'm working on an application that connects to a serial port. I have experience
I am working with a microcontroller that sends data to PC's serial port according

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.