base64 encoding


Results 1 to 9 of 9

Thread: base64 encoding

Threaded View

  1. #3
    Join Date
    Nov 2005
    Location
    Perth, Australia
    Posts
    429


    Did you find this post helpful? Yes | No

    Default

    Was bored, so I figured i would give this a shot.

    Untested, so try at your own risk

    Code:
    STRING_LENGTH   CON 24 ; Must be divisable by 3
    
    in_string       VAR BYTE[STRING_LENGTH]
    in_byte         VAR BYTE         
    in_bit          VAR BYTE
    b64_string      VAR BYTE[STRING_LENGTH*4/3]
    b64_byte        VAR BYTE
    b64_bit         VAR BYTE
    temp            VAR BYTE
    
    in_byte=0                                               ; Start at byte 0
    in_bit=7                                                ; Most significant bit first
    
    FOR b64_byte=0 TO (STRING_LENGTH*4/3)-1                 ; For each base64 byte:
        b64_string[b64_byte]=0                              ; Clear the byte
        FOR b64_bit=5 TO 0 STEP -1                          ; For each bit 5 thru 0:
            temp=in_string[in_byte]                         ; Get the current input byte
            temp=temp >> in_bit                             ; Shift to get the current bit as bit 0
            temp=temp & 1                                   ; Clear the other bits
            temp=temp << b64_bit                            ; Shift to the correct output bit position
            b64_string[b64_byte]=temp|b64_string[b64_byte]  ; Add the bit to the base64 byte
            in_bit=in_bit-1                                 ; Next less significant bit
            IF in_bit=255 THEN                              ; If we past bit 0:
                in_bit=7                                    ; Set to bit 7 of the next byte
                in_byte=in_byte+1
            ENDIF
        NEXT b64_bit
        SELECT CASE b64_string[b64_byte]                    ; Convert to base64 coded ASCII
            CASE IS<=25                                     ; 0-25
                b64_string[b64_byte]=b64_string[b64_byte]+65; "A"-"Z"
            CASE IS<=51                                     ; 26-51
                b64_string[b64_byte]=b64_string[b64_byte]+71; "a"-"z"
            CASE IS<=61                                     ; 52-61
                b64_string[b64_byte]=b64_string[b64_byte]-4 ; "0"-"9"
            CASE 62                                         ; 62
                b64_string[b64_byte]=43                     ; "+"
            CASE 63                                         ; 63
                b64_string[b64_byte]=47                     ; "/"
        END SELECT
    NEXT b64_byte
    Last edited by Kamikaze47; - 8th April 2008 at 21:58.
    "I think fish is nice, but then I think that rain is wet, so who am I to judge?" - Douglas Adams

Similar Threads

  1. HSERIN and STR - character encoding
    By boban in forum mel PIC BASIC Pro
    Replies: 13
    Last Post: - 13th August 2008, 17:36
  2. Help with serin, serout, Manchester encoding
    By oneohthree in forum mel PIC BASIC Pro
    Replies: 30
    Last Post: - 5th April 2007, 13:31
  3. Manchester encoding
    By jonathanRF in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 8th February 2007, 19:05
  4. Manchester encoding
    By jonathanRF in forum Off Topic
    Replies: 3
    Last Post: - 8th February 2007, 18:56
  5. Turbo Encoding with Pic Basic Pro
    By obaskirt in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 15th November 2006, 09:22

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts