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
There's load of room for improvement. At least it works!Code:<font color="#000000"> @ __CONFIG _HS_OSC & _LVP_OFF & _WDT_OFF <font color="#000080">DEFINE </font>OSC 20 <font color="#000080">DEFINE </font>HSER_RCSTA 90h <font color="#008000">' Enable serial port & 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>"Man is distinguished, not only by his reason, but by this singular passion "<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." </font>String2 <font color="#000080">DATA </font>0 BASE64Table <font color="#000080">DATA </font>@191, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=" 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>["Len=",<font color="#000080">DEC </font>Len,13,10,_ "Original string",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, "Encoded String",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]>>2 Encoded[1]=((buffer[0]&3) <<4) | (buffer[1]>>4) <font color="#000080">IF </font>buffer[1] <font color="#000080">THEN </font>Encoded[2]=((buffer[1]&$0F)<<2) | (buffer[2]>>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] & %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 still the "convenient" part to do... if really needed and asked.







Bookmarks