Byte Stuffing in Computer Networks: Worked Framing Example and Exam Traps
Learn a precise byte-stuffing convention, trace a payload containing both FLAG and ESC, reverse it safely, and calculate frame overhead and transmission time.
KnowledgeGate Team
Exam prep & CS education

A flag byte makes frame boundaries easy to see until the same value appears inside the payload. Byte stuffing removes that ambiguity, but traces become error-prone when the data contains both the flag and the escape byte. In the worked frame, five payload bytes become eight body bytes and ten transmitted bytes; de-stuffing restores the original five. A protocol may use different reserved values or transform escaped bytes, so define its grammar before tracing.
1. Why the Data Link Layer Needs Framing
Framing divides a stream of bits or bytes into units that a receiver can recognise. At the data link layer, delimiters mark where a payload frame starts and ends. For the four-method comparison, character-count failure, and a compact four-byte escape trace, use DLL Framing in Computer Networks: Methods, Bit Stuffing and Worked Examples. The five-byte trace below isolates byte stuffing, decoder state, frame-size arithmetic, and transmission time.
Let FLAG = 0x7E. In wire sequence 0x7E 0x41 0x7E 0x42 0x7E, the intended payload might be 0x41 0x7E 0x42. Its middle 0x7E is data, so a receiver cannot treat every copy as a delimiter.
Byte stuffing, also called character stuffing, resolves this in byte-oriented framing by escaping reserved data bytes. The broader CS Fundamentals for Exams & Placements path connects framing with the rest of the core-CS syllabus.
2. The Exact Byte-Stuffing Rule
Fix the grammar before tracing any bytes:
FLAG = 0x7EandESC = 0x1B.One unescaped
FLAGstarts the frame. The next unescapedFLAGends it.Inside the body, the sender prefixes every payload occurrence of
FLAGorESCwith oneESC.The receiver reads the byte after an interior
ESCliterally.This convention performs no XOR or other transformation.
Compact sender logic is:
for each payload byte b:
if b == FLAG or b == ESC: emit ESC
emit b
emit FLAG before and after the resulting bodyAfter opening, the receiver starts with escaped = false. An unescaped ESC sets it to true. The next byte is appended literally and clears it. An unescaped FLAG ends the frame.
If a payload contains n bytes and r total reserved-byte occurrences, the stuffed body has n + r bytes. The complete frame has n + r + 2 bytes. Here r counts occurrences, not the number of distinct reserved values.
3. Worked Encoding: Stuff a Payload Containing FLAG and ESC
Use the five-byte payload 0x41 0x7E 0x1B 0x42 0x7E, visually A, FLAG, ESC, B, FLAG. Scan it from left to right:
Emit ordinary byte
0x41.Encode the first data
0x7Eas0x1B 0x7E.Encode the data
0x1Bas0x1B 0x1B.Emit ordinary byte
0x42.Encode the final data
0x7Eas0x1B 0x7E.
The stuffed body is 0x41 0x1B 0x7E 0x1B 0x1B 0x42 0x1B 0x7E.
The complete transmitted frame is:
0x7E | 0x41 0x1B 0x7E 0x1B 0x1B 0x42 0x1B 0x7E | 0x7E
The payload contains three reserved occurrences, so the body grows from n = 5 to n + r = 8 bytes. Two delimiters make 10 transmitted bytes. Stuffing adds 3/5 = 60% relative to the payload. Including delimiters adds five bytes, so the complete frame is 10/5 = 2 times the payload size. A payload this rich in reserved values expands sharply: stuffing alone adds 60%, whereas payloads with fewer reserved bytes add less.

4. Worked De-stuffing: Recover the Original Five Bytes
Feed that frame to the receiver. The first 0x7E opens it. In the body, 0x41 is copied, 0x1B 0x7E produces 0x7E, 0x1B 0x1B produces 0x1B, and 0x42 is copied. The next 0x1B 0x7E produces 0x7E. The final unescaped 0x7E closes the frame.
The recovered payload is 0x41 0x7E 0x1B 0x42 0x7E, byte-for-byte identical to the input. The receiver removes only an ESC acting as an escape prefix. It retains the following byte even when that byte equals FLAG or ESC.
If the body ends with a lone ESC before termination or lost input, this decoder should report an incomplete escape sequence. A real protocol defines its recovery rule.

5. Byte Stuffing Versus Bit Stuffing and Length-Based Framing
Byte stuffing escapes reserved byte values. Bit stuffing inserts a bit after a specified run, even when a flag pattern crosses a byte boundary. Length-based framing states the frame's byte count, but corruption of that count can disturb synchronisation without recovery mechanisms.
With FLAG = 0x7E and ESC = 0x1B, payload byte 0x7E becomes 0x1B 0x7E. In a common HDLC-style bit-stuffing exercise, data 01111110 gets a 0 after the first run of five consecutive 1s, producing 011111010. Do not apply that five-ones rule to bytes.
Byte-stuffing grammars vary. If a question specifies DLE/STX/ETX, duplicated DLE, or XOR after an escape, write that grammar first.
6. A Frame-Size and Transmission-Time Numerical
Suppose n = 100 bytes. Seven payload bytes equal FLAG and four equal ESC, so r = 7 + 4 = 11. Use the same prefix rule, two one-byte delimiters, and a decimal 1 Mbps link. Ignore headers, trailers, propagation delay, and inter-frame gaps.
Stuffed body:
n + r = 100 + 11 = 111 bytes.Complete frame:
111 + 2 = 113 bytes = 904 bits.Stuffing overhead:
11/100 = 11%of payload size.Total framing overhead:
(11 + 2)/100 = 13%, or 13 added bytes.Raw payload time:
800/1,000,000 s = 800 microseconds.Complete-frame time:
904/1,000,000 s = 904 microseconds.Added wire time:
904 - 800 = 104 microseconds.
Transmission time is the time required to put the bits on the link. Add propagation delay only if a question asks for total delay and supplies enough information.
7. How Exams Test Byte Stuffing, and the Traps to Avoid
Questions commonly ask you to encode a payload, de-stuff a wire sequence, count inserted bytes, find frame length, distinguish data from delimiters, or compare byte and bit stuffing. Byte stuffing is part of Data Link Layer framing in Computer Networks, so you can practise these traces alongside the other framing topics.
Watch these traps:
Do not stuff the two boundary delimiters.
Stuff an original payload
ESCas well as a payloadFLAG.Under this convention,
ESC ESCrecovers one literalESC, not two.Count every reserved occurrence.
Never apply bit-level five-ones logic to a byte trace.
Follow the grammar in the question instead of assuming these values.
For a rapid check, take FLAG = F0, ESC = EE, and payload F0 EE 33. The stuffed body is EE F0 EE EE 33. The complete frame is F0 | EE F0 EE EE 33 | F0, which is seven bytes. Use the Computer Networks MCQs hub for broader practice.
8. Byte Stuffing in One Minute and the Next Step
Keep this five-line checklist:
Delimiters mark frame boundaries.
Reserved payload bytes need escaping.
The receiver treats the byte after
ESCliterally.With this convention,
npayload bytes andrreserved occurrences producen + r + 2transmitted bytes.The grammar stated in the question overrides memorised constants.
Self-check: remove payload 0x1B, leaving 0x41 0x7E 0x42 0x7E. Now n = 4 and r = 2, so the body has 6 bytes and the complete frame has 8. Encode it before checking your result: 0x7E | 0x41 0x1B 0x7E 0x42 0x1B 0x7E | 0x7E.
For a structured GATE-oriented path, continue with GATE Guidance by Sanchit Sir. For a broader core-CS route, use Zero to Hero Complete CS Course. The key next step is simple: write the grammar first, then trace every byte without skipping the escape state.
Keep learning

Computer Networks Hardware Basics: Devices, Domains and Worked Examples
Learn what hubs, switches, routers, gateways and access points actually do, then count network domains and trace frames through a two-LAN topology.

Data Link Layer Framing Explained: Byte Stuffing, Bit Stuffing and a Cross-Concept Numerical
Frame one four-byte payload in two ways, decode it, and then reuse the verified frame sizes in link-load and Stop-and-Wait calculations.

Go-Back-N Protocol Explained: Sliding Windows, Worked Numericals and Exam Traps
Trace Go-Back-N through a lost frame, calculate its legal window and link utilisation, and avoid the ACK and wrap-around traps that spoil numericals.

Network Protection Explained: Firewalls, Segmentation and a Worked ACL Example
Learn a reusable trust-boundary model for network protection, then trace allowed and blocked traffic through a five-zone web service and its ordered firewall rules.