Algorithm Help

This is a discussion on Algorithm Help within the Tech Board forums, part of the Community Boards category; Hi, I'm trying to write a decoder for Yenc, the compression algorithm is as follows: Code: 1. Fetch a character ...

  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
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,667
    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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

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, 01:30 AM
  2. Replies: 4
    Last Post: 12-10-2006, 06: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, 09:33 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21