0

I want to write a small script in python which does byte stuffing/unstuffing, since I have no idea how to do any of it Im just going for stuffing first.

So, the goal is simple, there is a function with three inputs, something like this: ByteStuffing(flagbyte, escapebyte, frame) and output with flag bytes in the beginning and end, and stuffed frame in between.

so lets say my flag byte is Z, escape byte is A and frame LEONARDO

ByteStuffing(Z,A,LEONARDO) = ZLEONAARDOZ

p.s. escape byte also escapes flag characters in frame, not just escape bytes itself.

unstuffing is just reverse function.

now, i am really not familiar with programming, just getting started so I am really not very creative when it comes to coding, that's why I want to start with this small 'projects', can anybody give me ideas how to get started with this problem?

p.s Python version 2.7.12

EDIT: from Keerthana Prabhakaran's answer i ended up with this.

def applyByteStuffing(flagbyte, escapebyte, payload):

flagbyte = ("Z")
escapebyte = ("A")


x = payload.replace (escapebyte, escapebyte*2)
y = x.replace (flagbyte, escapebyte+flagbyte)
print (flagbyte + y + flagbyte)
return;

if i try to test it like this:

def test():


assert applyByteStuffing("Z", "A", "TAZZA") == "ZTAAAZAZAAZ" 

if __name__ == '__main__':

test()

I am getting assertion error. has anyone idea where my assertion error is happening?

just trying out trying out this: applyByteStuffing("Z" , "A" , "TAZZA")

gives me the correct output: ZTAAAZAZAAZ

2
  • @NickA I have no idea what is bit packing. I know byte stuffing and bit stuffing and I mean byte stuffing, not bit Commented May 22, 2017 at 14:29
  • ah right enough, I've never heard of byte stuffing before Commented May 22, 2017 at 14:30

2 Answers 2

1

You can use str.replace() for this purpose. Replace the escapebyte with another string with the duplicate, and stuff the resulting string with the flagbyte!

>>> flagbyte + frame.replace(escapebyte,escapebyte*2)+ flagbyte
'ZLEONAARDOZ'

For the updated question, you need to return something from applyByteStuffing so that the return value can be asserted against the string on RHS.

def applyByteStuffing(flagbyte, escapebyte, payload):
    x = payload.replace (escapebyte, escapebyte*2)
    y = x.replace (flagbyte, escapebyte+flagbyte)
    return flagbyte + y + flagbyte #return value

And then assert!

assert applyByteStuffing('Z','A','LEONARDO') == 'ZLEONAARDOZ'
Sign up to request clarification or add additional context in comments.

2 Comments

thanks for the idea, i edited the question before, saying that i also need to escape flag bytes. so i expanded your solution a little bit and it works overall. I will edit my question now with new problem :)
Glad that it helped! I've updated my answer for the assertion error!
1
bits=[1,0,0,1,1,1,1,1,0,1,1,0]
stuffed=[]
count=0
for i in range(len(bits)):
    if bits[i]==1:
        count=count+1
        stuffed.append(bits[i])
    elif bits[i]!=1:
        count=0
        stuffed.append(bits[i])
    if count==5:
        stuffed.insert(i+1,0)
print stuffed
#Take any input the code works perfectly. #Tested

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.