首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

无聊写个base64编码函数,大伙帮忙看看出了啥有关问题哈

2012-03-07 
无聊写个base64编码函数,大伙帮忙看看出了啥问题哈!本人闲着无聊自已写了个base64编码的函数,但是跟别人的

无聊写个base64编码函数,大伙帮忙看看出了啥问题哈!
本人闲着无聊自已写了个base64编码的函数,但是跟别人的编码结果不一致,实在郁闷,看了半天也不知道问题出在了哪里,大伙帮忙瞧瞧吧,汗。。。
代码如下:

#include   <stdio.h>
#include   <stdlib.h>


char   base64_alphabet[]   =   "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/= ";

void   Base64Encode(char   *   sour,   char   *   desc,   long   len)
{
          ldiv_t   div_result;
          long   count;
          div_result   =   ldiv(len,   3);
          for   (count   =   0;   count   <   div_result.quot;   ++count)
          {
                  *desc++   =   base64_alphabet[(sour[0]   > >   2)   &   0x3f];
                  *desc++   =   base64_alphabet[((sour[0]   < <   4)   |   ((unsigned)sour[1]   > >   4))   &   0x3f];
                  *desc++   =   base64_alphabet[((sour[1]   < <   2)   |   ((unsigned)sour[2]   > >   6))   &   0x3f];
                  *desc++   =   base64_alphabet[sour[2]   &   0x3f];
                  sour   +=   3;
          }
          if   (div_result.rem   ==   2)
          {
                *desc++   =   base64_alphabet[(sour[0]   > >   2)   &   0x3f];
                *desc++   =   base64_alphabet[((sour[0]   < <   4)   |   ((unsigned)sour[1]   > >   4))   &   0x3f];
                *desc++   =   base64_alphabet[(sour[1]   < <   2)   &   0x3c];
                *desc++   =   '= ';
                 
          }
          else   if   (div_result.rem   ==   1)
          {
                *desc++   =   base64_alphabet[(sour[0]   > >   2)   &   0x3f];
                *desc++   =   base64_alphabet[(sour[0]   < <   4)   &   0x30];
                *desc++   =   '= ';
                *desc++   =   '= ';
          }        
          *desc   =   '\0 ';  
}

int   main(void)
{
        char   *sour   =   "谢谢测试! ";
        char   dest[100];
        Base64Encode(sour,   dest,   strlen(sour));
        puts(dest);


          system( "pause ");
          return   0;
}
 
我的输出结果是:07/Qu7/iy9Qh
老罗的程序(http://www.luocong.com/articles/show_article.asp?Article_ID=17)输出结果是:0LvQu7LiytQh
这里(http://www.51windows.net/myjs/?u=/hw/asp/jsview.asp?id=134)的输出结果是:6LCi6LCi5rWL6K+VIQ==
本来很有信心,但是看到这样的结果什么信心都没了,写个这么简单的东东都这样子,真像是白学了.



[解决办法]
关注
[解决办法]
php里面有源代码:
/*
+----------------------------------+
| PHP HTML Embedded Scripting Language Version 3.0 |
+----------------------------------+
| Copyright (c) 1997-2000 PHP Development Team (See Credits file) |
+----------------------------------+
| This program is free software; you can redistribute it and/or modify |
| it under the terms of one of the following licenses: |
| |
| A) the GNU General Public License as published by the Free Software |
| Foundation; either version 2 of the License, or (at your option) |
| any later version. |
| |
| B) the PHP License as published by the PHP Development Team and |
| included in the distribution in the file: LICENSE |
| |
| This program is distributed in the hope that it will be useful, |
| but WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| GNU General Public License for more details. |
| |
| You should have received a copy of both licenses referred to here. |
| If you did not, or have any questions about PHP licensing, please |
| contact core@php.net. |
+----------------------------------+
| Author: Jim Winstead (jimw@php.net) |
+----------------------------------+
*/
/* $Id: base64.c,v 1.32 2000/01/01 04:31:14 sas Exp $ */

#include <string.h>

#include "php.h "


#include "internal_functions.h "
#include "base64.h "

static char base64_table[] =
{ 'A ', 'B ', 'C ', 'D ', 'E ', 'F ', 'G ', 'H ', 'I ', 'J ', 'K ', 'L ', 'M ',
'N ', 'O ', 'P ', 'Q ', 'R ', 'S ', 'T ', 'U ', 'V ', 'W ', 'X ', 'Y ', 'Z ',
'a ', 'b ', 'c ', 'd ', 'e ', 'f ', 'g ', 'h ', 'i ', 'j ', 'k ', 'l ', 'm ',
'n ', 'o ', 'p ', 'q ', 'r ', 's ', 't ', 'u ', 'v ', 'w ', 'x ', 'y ', 'z ',
'0 ', '1 ', '2 ', '3 ', '4 ', '5 ', '6 ', '7 ', '8 ', '9 ', '+ ', '/ ', '\0 '
};
static char base64_pad = '= ';

unsigned char *_php3_base64_encode(const unsigned char *string, int length, int *ret_length) {
const unsigned char *current = string;
int i = 0;
unsigned char *result = (unsigned char *)emalloc(((length + 3 - length % 3) * 4 / 3 + 1) * sizeof(char));

while (length > 2) { /* keep going until we have less than 24 bits */
result[i++] = base64_table[current[0] > > 2];
result[i++] = base64_table[((current[0] & 0x03) < < 4) + (current[1] > > 4)];
result[i++] = base64_table[((current[1] & 0x0f) < < 2) + (current[2] > > 6)];
result[i++] = base64_table[current[2] & 0x3f];

current += 3;
length -= 3; /* we just handle 3 octets of data */
}

/* now deal with the tail end of things */
if (length != 0) {
result[i++] = base64_table[current[0] > > 2];
if (length > 1) {
result[i++] = base64_table[((current[0] & 0x03) < < 4) + (current[1] > > 4)];
result[i++] = base64_table[(current[1] & 0x0f) < < 2];
result[i++] = base64_pad;
}
else {
result[i++] = base64_table[(current[0] & 0x03) < < 4];
result[i++] = base64_pad;
result[i++] = base64_pad;
}
}
if(ret_length) {
*ret_length = i;
}
result[i] = '\0 ';
return result;
}
[解决办法]
帮顶

热点排行