Michael Cotherman wrote: >>....... is the decoding code, it does what you >>describe below. It is adding the zeros back in, that >>is the dst.append('\0') >> >>Kent > > Really??? I am a noob, but it looks like it only puts > a \0 at the end if code (the length) is less than 255, > as the if statement is out of the for loop? this would > be part of the coding algorithm, no?
It puts 0 at the end of each segment if the length of the segment is less than FF. In the original C code, the for loop is copying the data of a segment. The while loop in both codes is looping over segments. Here is my understanding of the algorithm (with maybe a few fine details left out): To encode: - look for the first 0 byte in the data - if it is more than FE bytes from the start, write out and FF and the first FE bytes. This is a segment that *doesn't* end with a 0. - otherwise write out (1+the number of bytes before the 0), then all the bytes before the zero. Skip the input pointer to the byte after the 0. - repeat until no more data To decode: - get the next byte from the data. This is the count - get the next (count-1) bytes from the data and append to the output - if count < FF then this segment ends with a 0, so append a 0 to the output. - repeat until no more data Oh, I see you already wrote the same thing in your previous email! Kent > > In that theme of writing, here is what I would do to > make it decode: > > > def UnStuffData(src,dst,len): > if code == 0xFF: > pass > else: > x = src(0) > for code in src: > for i in range(1,code): > if i == x: > dst.append('\0') > x = src(i) > else: > dst.append(i) > > dst.pop(0) #remove first byte > # if code < 0xff > # dst.append('\0') > > > > > -mike > > > > --- Kent Johnson <[EMAIL PROTECTED]> wrote: > > >>Michael Cotherman wrote: >> >>>def UnStuffData(src,dst,len): >>> >>> for code in src: >>> for i in range(1,code): >>> dst.append(i) >>> >>> if code < 0xff >>> dst.append('\0') >>> >>>the above is the below code uncommented... >>> it(and the original code) just seem to find the >> >>end >> >>>and puts a zero there... >>> >>>I do not see the existing zeroes replaced..... >> >>this is >> >>>kinda why I wanted to start from the >> >>specification... >> >>The above is the decoding code, it does what you >>describe below. It is adding the zeros back in, that >>is the dst.append('\0') >> >>Kent >> >> >>>decoding is: >>>pop the first byte of the packet off and place >>>subsequent bytes into the output until you get to >> >>the >> >>>byte pointed to by the popped byte. When you reach >>>that byte, read its value as the new 'relative' >>>pointer, and copy x00 to the output. Proceed as >> >>above, >> >>>placing bytes to the output until the relative >> >>pointer >> >>>is reached or the end of the packet occurrs. >>> >>>I guess I need to look if I wish for this part of >> >>the >> >>>program to handle the input as one big existing >>>string, or work on it a byte at a time as it comes >> >>in. >> >>> >>> >>>-mike c >> >>_______________________________________________ >>Tutor maillist - Tutor@python.org >>http://mail.python.org/mailman/listinfo/tutor >> > > > > > > __________________________________ > Yahoo! Mail - PC Magazine Editors' Choice 2005 > http://mail.yahoo.com > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > > _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor