标准的Base64算法描述

时间:2014-12-20 00:55:10   收藏:0   阅读:194
作者:zhanhailiang 日期:2014-12-19
算法描述:

Base64要求把每三个8Bit的字节转换为四个6Bit的字节(38 = 46 = 24),然后把6Bit再添两位高位0,组成四个8Bit的字节,也就是说,转换后的字符串理论上将要比原来的长1/3。

以上算法描述引用自:http://baike.baidu.com/link?url=lriFEiUvmROqg0a0R0DZS0t8gEmIQRnJ13eSGZuYFFOyUDSrhY824dE7zjTbhSA5rpp2NiDn2-mCoLPbPlY1q_#3_3

算法实现:

从PHP/ext/standard/base64.c中剥离出base64_encode的实现方法,代码如下:https://github.com/billfeller/billfeller.github.io/tree/master/code/base64


测试:
[root@~/wade/git/billfeller.github.io/code/base64]# gcc -o base64 base64.c 
[root@~/wade/git/billfeller.github.io/code/base64]# ./base64 "abcd*"
YWJjZCo=
[root@~/wade/git/billfeller.github.io/code/base64]# /usr/local/php/bin/php -r ‘echo base64_encode("abcd*").PHP_EOL;‘
YWJjZCo=
评论(0)
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!