ASPN ActiveState Programmer Network  
ActiveState, a division of Sophos
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups
Submit Recipe
My Recipes

All Recipes
All Cookbooks


View by Category

Title: z_string.py
Submitter: Stephen Chappell (other recipes)
Last Updated: 2006/07/07
Version no: 1.0
Category: Algorithms

 

Not Rated yet


Description:

This module provides several functions that can
encode and decode a string. It also has a
function called partition for designing faster
algorithms. For sample usage of this module,
z_service.py provides example code.

Source: Text Source

'''Module for string manipulation.

This module provides several functions that
can encode, decode, and convert strings.'''

__version__ = 1.3

################################################################################

def string_code(string):
    'Convert from string to code.'
    return number_code(string_number(string))

def code_string(code):
    'Convert from code to string.'
    return number_string(code_number(code))

def string_number(string):
    'Convert from string to number.'
    number = 1L
    for character in string:
        number <<= 8
        number += ord(character)
    return number

def number_code(number):
    'Convert from number to code.'
    code = ''
    while number:
        code = chr(number % 255 + 1) + code
        number /= 255
    return code

def code_number(code):
    'Convert from code to number.'
    number = 0L
    for character in code:
        number *= 255
        number += ord(character) - 1
    return number

def number_string(number):
    'Convert from number to string.'
    string = ''
    while number > 1:
        string = chr(number & 0xFF) + string
        number >>= 8
    return string

def partition(string, size):
    'Partitions a string into substrings.'
    for index in range(0, len(string), size):
        yield string[index:index+size]

################################################################################

if __name__ == '__main__':
    import sys
    print 'Content-Type: text/plain'
    print
    print file(sys.argv[0]).read()

Discussion:

This recipe used an algorithm that may be difficult
to replicate in some languages, but the approach is
rather unique in how information is encoded. The
general idea behind the procedure is to convert the
text into a number and then rewrite it in a different
base. Strings can be thought of as base 256 numbers
and can be stored as such using long numbers.
Converting those numbers into a lower base is as
simple as some modular arithmetic and divition. What
results is a string that can be very hard to figure
out for people trying to backward-engineer the encoding.



Add comment

No comments.



Highest rated recipes:

1. A simple XML-RPC server

2. Web service accessible ...

3. IPy Notify

4. Changing return value ...

5. Quantum Superposition

6. Pickle objects under ...

7. Generalized delegates ...

8. Reorder a sequence (uses ...

9. Setting Win32 System ...

10. ObjectMerger




Privacy Policy | Email Opt-out | Feedback | Syndication
© 2006 ActiveState Software Inc. All rights reserved.