Thread: Algorithm Help

  1. #1
    Registered User (TNT)'s Avatar
    Join Date
    Aug 2001
    Location
    UK
    Posts
    339

    Algorithm Help

    Hi,

    I'm trying to write a decoder for Yenc, the compression algorithm is as follows:

    Code:
     1. Fetch a character from the input stream.  
     2. Increment the character's ASCII value by 42, modulo 256 
     3. If the result is a critical character (as defined in the previous
        section), write the escape character to the output stream and increment
        character's ASCII value by 64, modulo 256.  
     4. Output the character to the output stream.  
     5. Repeat from start.
    So in terms of compression my pseudo code looks something like this:

    Code:
    Result = (ASCII+42) % 256
    If Result == CriticalValue
       WriteOut(CriticalValue) 
       Result = (CriticalValue + 64) % 256
    
    WriteOut(Result)
    Now for reversal and getting the orignal ASCII code out I have no idea how to approach it as i don't really understand modulo maths, could anyone lend a hand with some reversal pseudo code?

    Thanks alot for any help,.

    Jack
    TNT
    You Can Stop Me, But You Cant Stop Us All

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Sorta like what you would do with algebra:
    Code:
    ascii = result % 256 - 42;
    if ( iscritical(ascii) ) 
    {
      WriteOut(ascii);
      result = ascii % 256 - 64;
    }
    WriteOut(result);
    Last edited by whiteflags; 07-27-2007 at 11:04 AM.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    result % 256 - 42 can be less than 0
    so (because result is between 0 and 255 %256 will have no effect)
    the proper way will be

    (result - 42 +256)%256
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Implement of a Fast Time Series Evaluation Algorithm
    By BiGreat in forum C Programming
    Replies: 7
    Last Post: 12-04-2007, 02:30 AM
  2. Replies: 4
    Last Post: 12-10-2006, 07:08 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM