I put here in public domain, for Clipper and [x]Harbour developpers,
some functions i have created to translate strings in Base64 encodage
and reverse. This will help you in some cases, like to create your own
TSMTP class with Autentification support, necessary to communicate
with more in more SMTP providers...
To help me, you can if this please you, and if you have a web site,
put a link to my site to improve my google ratio. Thanks in advance.
Best Regards.
- Code: Select all Expand view
* BASE64.PRG
* Creation le 30/12/2008
* Auteur Badara Thiam
*******************
FUNCTION StrToBase64( cTexte )
*******************
* Conversion en base 64 de la chaine cTexte
LOCAL cTexte64 := ""
LOCAL X
LOCAL cHex
DO WHILE !( cTexte == "" )
cHex := ""
FOR X := 1 TO 3
* Conversion de chaque caractère en chaine binaire de 8 octets
cHex += CarToBin( LEFT(cTexte, 1) )
IF LEN(cTexte) > 1
cTexte := SUBSTR(cTexte, 2)
ELSE
cTexte := ""
EXIT
ENDIF
NEXT X
FOR X := 1 TO 4
IF SUBSTR(cHex, ( (X - 1) * 6) + 1 ) == ""
cTexte64 += "="
ELSE
cTexte64 += Carac64( "00" + SUBSTR(cHex, ( (X - 1) * 6) + 1, 6 ) )
ENDIF
NEXT X
ENDDO
RETURN cTexte64
*********************
FUNCTION Base64ToStr( cTexte64 )
*********************
* décodage d'un texte codé en base 64
LOCAL cTexte := ""
LOCAL X
LOCAL cHex
LOCAL cCar
DO WHILE !( cTexte64 == "" )
cCar := LEFT(cTexte64,4)
cHex := ""
FOR X := 1 TO 4
IF SUBSTR(cCar, X, 1 ) != "="
cHex += Hex64( SUBSTR(cCar, X, 1 ) )
ELSE
EXIT
ENDIF
NEXT X
FOR X := 1 TO 3
IF SUBSTR(cHex, ( (X - 1) * 8) + 1 ) == ""
EXIT
ELSE
cTexte += BinToCar( SUBSTR(cHex, ( (X - 1) * 8) + 1, 8 ) )
ENDIF
NEXT X
IF LEN(cTexte64) > 4
cTexte64 := SUBSTR(cTexte64, 5)
ELSE
cTexte64 := ""
ENDIF
ENDDO
RETURN cTexte
****************
FUNCTION Carac64( cBin )
****************
* Renvoie le caractère correspondant en base 64
LOCAL nPos := ASC( BinToCar( @cBin ) ) + 1
RETURN SUBSTR( "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/", nPos, 1)
**************
FUNCTION Hex64( carac64 )
**************
* Renvoie le caractère correspondant en base 64
LOCAL cCodeAsc := CHR( AT(carac64, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" ) -1 )
RETURN SUBSTR( CarToBin( @cCodeAsc ) , 3, 6)
*****************
FUNCTION CarToBin( carac, lInverse )
*****************
* Renvoie le caractère correspondant dans une chaine binaire (composée de 0 et 1) de 8 bits
#define cHexa "0123456789ABCDEF"
#define aBin {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", ;
"1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111" }
LOCAL cToHex
IF EMPTY( lInverse )
* Retourne la chaine binaire en ayant reçu le caractère ASCII
cToHex := str2Hex( carac )
RETURN aBin[ AT( LEFT(cToHex,1), cHexa ) ] + aBin[ AT( SUBSTR(cToHex,2), cHexa ) ]
ELSE
* Retourne le caractère ASCII en ayant reçu la chaine binaire
cToHex := SUBSTR(cHexa, ASCAN(aBin, LEFT(carac,4 ) ), 1 ) ;
+ SUBSTR(cHexa, ASCAN(aBin, SUBSTR(carac,5,4 ) ), 1 )
RETURN Hex2str( cToHex )
ENDIF
RETURN NIL
*****************
FUNCTION BinToCar( cBin )
*****************
RETURN CarToBin( @cBin, .T. )