base64 encoding


Closed Thread
Results 1 to 9 of 9

Thread: base64 encoding

Hybrid View

  1. #1
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    No one?

    i had a 1/2 hour to kill so i did this one. No matter if your string is dividable by three or not, the software will adjust it. I've compare my results with the following online converter tool

    http://makcoder.sourceforge.net/demo/base64.php

    Everything seems ok. Personnal and deliberate decision from myself to remove the comments... oh i'm a monster

    You just need to figure out

    This assume your PIC have a 256 bytes EEPROM. I used a dusty F877

    Code:
    <font color="#000000">        @   __CONFIG _HS_OSC &amp; _LVP_OFF &amp; _WDT_OFF
            <font color="#000080">DEFINE </font>OSC 20
    
            <font color="#000080">DEFINE </font>HSER_RCSTA 90h <font color="#008000">' Enable serial port &amp; continuous receive
            </font><font color="#000080">DEFINE </font>HSER_TXSTA 24h <font color="#008000">' Enable transmit, BRGH = 1
            </font><font color="#000080">DEFINE </font>HSER_SPBRG 129 <font color="#008000">' 9600 Baud @ 20MHz, 0.16%
            </font><font color="#000080">DEFINE </font>HSER_CLROERR 1 <font color="#008000">' Clear overflow automatically
         
            </font>Len             <font color="#000080">VAR BYTE
            </font>ByteA           <font color="#000080">VAR BYTE
            </font>buffer          <font color="#000080">VAR BYTE</font>[3]
            Encoded         <font color="#000080">VAR BYTE</font>[4]
            EncodedCounter  <font color="#000080">VAR BYTE
            
            </font>CounterA        <font color="#000080">VAR BYTE
            </font>CounterB        <font color="#000080">VAR BYTE
            
    
    </font>String1     <font color="#000080">DATA </font>&quot;Man is distinguished, not only by his reason, but by this singular passion &quot;<font color="#008000">;from other animals, which is a lust of the mind, that by a perseverance of delight in the continued and indefatigable generation of knowledge, exceeds the short vehemence of any carnal pleasure.&quot;
    </font>String2     <font color="#000080">DATA </font>0
    
    BASE64Table <font color="#000080">DATA </font>@191, &quot;ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=&quot;
    
            Len = String2 - String1 
            
            <font color="#000080">IF </font>(Len//3) <font color="#000080">THEN
                </font>ByteA=3-(Len//3)
                <font color="#000080">FOR </font>CounterA=0 <font color="#000080">TO </font>(ByteA-1)
                    <font color="#000080">WRITE </font>(Len+CounterA),0
                    <font color="#000080">NEXT
                </font>Len=Len+ByteA
                <font color="#000080">ENDIF
            
            HSEROUT </font>[&quot;Len=&quot;,<font color="#000080">DEC </font>Len,13,10,_
                     &quot;Original string&quot;,13,10]
            
            <font color="#000080">FOR </font>CounterA=0 <font color="#000080">TO </font>(Len-1)
                <font color="#000080">READ </font>CounterA,ByteA
                <font color="#000080">IF </font>ByteA <font color="#000080">THEN 
                    HSEROUT </font>[ByteA]
                    <font color="#000080">ENDIF
                NEXT
            
            HSEROUT </font>[13,10, &quot;Encoded String&quot;,13,10]
            EncodedCounter=0     
           
            <font color="#000080">FOR </font>CounterA=0 <font color="#000080">TO </font>(Len-3) <font color="#000080">STEP </font>3
                <font color="#000080">READ </font>CounterA, buffer[0]
                <font color="#000080">READ </font>(CounterA+1), buffer[1]
                <font color="#000080">READ </font>(CounterA+2), buffer[2]
                Encoded[0]=buffer[0]&gt;&gt;2
                Encoded[1]=((buffer[0]&amp;3) &lt;&lt;4) | (buffer[1]&gt;&gt;4)
                
                <font color="#000080">IF </font>buffer[1] <font color="#000080">THEN
                    </font>Encoded[2]=((buffer[1]&amp;$0F)&lt;&lt;2) | (buffer[2]&gt;&gt;6)
                    <font color="#000080">ELSE
                        </font>Encoded[2]=64
                    <font color="#000080">ENDIF
                
                IF </font>buffer[2] <font color="#000080">THEN
                    </font>Encoded[3]=buffer[2] &amp; %00111111
                    <font color="#000080">ELSE
                        </font>Encoded[3]=64
                    <font color="#000080">ENDIF
                </font>EncodedCounter=EncodedCounter+4
                
                <font color="#000080">FOR </font>CounterB=0 <font color="#000080">TO </font>3
                    <font color="#000080">READ </font>(Base64Table+Encoded[CounterB]),ByteA
                    <font color="#000080">HSEROUT </font>[ByteA]
                    <font color="#000080">NEXT
                NEXT
            
            STOP
            END
    </font>
    There's load of room for improvement. At least it works!

    There's still the "convenient" part to do... if really needed and asked.
    Last edited by mister_e; - 9th April 2008 at 04:12.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

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


    Did you find this post helpful? Yes | No

    Default

    Touché

    "I think fish is nice, but then I think that rain is wet, so who am I to judge?" - Douglas Adams

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


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by mister_e View Post
    Personnal and deliberate decision from myself to remove the comments.
    Do you mean:

    Personal and deliberate decision from myself to be lazy and not write any comments in the first place.
    :P
    "I think fish is nice, but then I think that rain is wet, so who am I to judge?" - Douglas Adams

  4. #4
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    mmm time saving... maybe true
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  5. #5
    Join Date
    Jan 2007
    Posts
    39


    Did you find this post helpful? Yes | No

    Default Sorry for the late response

    Cus, I am very soory for the too late delay, I have a lot personal stuff to do and didn't had time for the playing.I see, that you have spent a lot of time to do it, it is nice inspiration for me to use your code. Thans a lot - it is both of you!!!

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