Python: Base43 string decoding

This is a reimplementation in python from:
import copy
class Base43:
    def __init__(self):
        self.base43Chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ$*+-./:'

    """long division with an arbitrary base."""
    def divMod(self,byteArr, base, divisor, startAt):
        result = copy.copy(byteArr)#.slice();
        remainder = 0
        temp = None;
        startAt = startAt if startAt else 0;
        for i in range(startAt, len(byteArr)):
            temp = remainder * base + byteArr[i]
            result[i] = temp / divisor
            remainder = temp % divisor;
        return {
            'div': result,
            'mod': remainder,

    def byteArrGreaterThan(self,byteArr, limit):
        length = len(byteArr)
        last = byteArr[length - 1]
        if last >= limit:
            return True
            for i in range(0, (length-1)):
                if byteArr[i] > 0:
                    return True
        return False;
   #input is a base-43 string, output is a hex string
    def decode(self,input):
        result = []
        input43 = []
        for i in range(0,len(input)): input43.append(i+100)

        for l in input:
            index = self.base43Chars.find(l)
            if (index < 0):
                raise Exception("Illegal character. Allowed characters are:" + self.base43Chars)
            #print "D: "+str(i)+' -> '+l+" -> "+str(index)
            input43[i] = index
            i += 1

        zeroCount = 0;
        pad = '';
        while zeroCount < len(input) and input43[zeroCount] == 0:
            zeroCount += 1;
            pad += '0';

        startAt = zeroCount

        while self.byteArrGreaterThan(input43, 256):
            r = self.divMod(input43, 43, 256, startAt);
            input43 = r['div'];


        r1 = ''
        ret = ''
        for r in result:
            if len(r1)<2:
                ret += "0"+r1
                ret += r1

        return pad + ret


Trackback specific URI for this entry

This link is not meant to be clicked. It contains the trackback URI for this entry. You can use this URI to send ping- & trackbacks from your own blog to this entry. To copy the link, right click and select "Copy Shortcut" in Internet Explorer or "Copy Link Location" in Mozilla.

No Trackbacks


Display comments as Linear | Threaded

No comments

Add Comment

You can use [geshi lang=lang_name [,ln={y|n}]][/geshi] tags to embed source code snippets.
Enclosing asterisks marks text as bold (*word*), underscore are made via _word_.
Standard emoticons like :-) and ;-) are converted to images.
E-Mail addresses will not be displayed and will only be used for E-Mail notifications.
To leave a comment you must approve it via e-mail, which will be sent to your address after submission.

To prevent automated Bots from commentspamming, please enter the string you see in the image below in the appropriate input box. Your comment will only be submitted if the strings match. Please ensure that your browser supports and accepts cookies, or your comment cannot be verified correctly.