user-0 提问: java 转C的问题

回复:
user: user-1
content: 直接转成C语言代码比较长。  引用 楼主 aaadddzxc 的回复:有没有对JAVA熟悉的，帮忙转下代码   current = (byte)(((a << 2) & 0xfc) | ((b >>> 4) & 3));  这个怎么表示？用C语言  完成代码如下：     import java.text.StringCharacterIterator; import java.text.CharacterIterator;  /**  * See the BASE64MailboxEncoder for a description of the RFC2060 and how  * mailbox names should be encoded.  This class will do the correct decoding  * for mailbox names.  *  * @author	Christopher Cotton  */  public class BASE64MailboxDecoder {          public static String decode(String original) { 	if (original == null || original.length() == 0) 	    return original;  	boolean changedString = false; 	int copyTo = 0; 	// it will always be less than the original 	char[] chars = new char[original.length()];  	StringCharacterIterator iter = new StringCharacterIterator(original); 	 	for(char c = iter.first(); c != CharacterIterator.DONE;  	    c = iter.next()) {  	    if (c == '&') { 		changedString = true; 		copyTo = base64decode(chars, copyTo, iter); 	    } else { 		chars[copyTo++] = c; 	    } 	} 	 	// now create our string from the char array 	if (changedString) { 	    return new String(chars, 0, copyTo); 	} else { 	    return original; 	}	     }       protected static int base64decode(char[] buffer, int offset, 				      CharacterIterator iter) { 	boolean firsttime = true; 	int leftover = -1;  	while(true) { 	    // get the first byte 	    byte orig_0 = (byte) iter.next(); 	    if (orig_0 == -1) break; // no more chars 	    if (orig_0 == '-') { 		if (firsttime) { 		    // means we got the string "&-" which is turned into a "&" 		    buffer[offset++] = '&'; 		} 		// we are done now 		break; 	    } 	    firsttime = false; 	     	    // next byte 	    byte orig_1 = (byte) iter.next();	     	    if (orig_1 == -1 || orig_1 == '-') 		break; // no more chars, invalid base64 	     	    byte a, b, current; 	    a = pem_convert_array[orig_0 & 0xff]; 	    b = pem_convert_array[orig_1 & 0xff]; 	    // The first decoded byte 	    current = (byte)(((a << 2) & 0xfc) | ((b >>> 4) & 3));  	    // use the leftover to create a Unicode Character (2 bytes) 	    if (leftover != -1) { 		buffer[offset++] = (char)(leftover << 8 | (current & 0xff)); 		leftover = -1; 	    } else { 		leftover = current & 0xff; 	    } 	     	    byte orig_2 = (byte) iter.next(); 	    if (orig_2 == '=') { // End of this BASE64 encoding 		continue; 	    } else if (orig_2 == -1 || orig_2 == '-') { 	    	break; // no more chars 	    } 	    	     	    // second decoded byte 	    a = b; 	    b = pem_convert_array[orig_2 & 0xff]; 	    current = (byte)(((a << 4) & 0xf0) | ((b >>> 2) & 0xf));  	    // use the leftover to create a Unicode Character (2 bytes) 	    if (leftover != -1) { 		buffer[offset++] = (char)(leftover << 8 | (current & 0xff)); 		leftover = -1; 	    } else { 		leftover = current & 0xff; 	    }  	    byte orig_3 = (byte) iter.next(); 	    if (orig_3 == '=') { // End of this BASE64 encoding 		continue; 	    } else if (orig_3 == -1 || orig_3 == '-') { 	    	break;  // no more chars 	    } 	     	    // The third decoded byte 	    a = b; 	    b = pem_convert_array[orig_3 & 0xff]; 	    current = (byte)(((a << 6) & 0xc0) | (b & 0x3f)); 	     	    // use the leftover to create a Unicode Character (2 bytes) 	    if (leftover != -1) { 		buffer[offset++] = (char)(leftover << 8 | (current & 0xff)); 		leftover = -1; 	    } else { 		leftover = current & 0xff; 	    }	     	} 	 	return offset;     }      /**      * This character array provides the character to value map      * based on RFC1521, but with the modification from RFC2060      * which changes the '/' to a ','.      */        // shared with BASE64MailboxEncoder     static final char pem_array[] = { 	'A','B','C','D','E','F','G','H', // 0 	'I','J','K','L','M','N','O','P', // 1 	'Q','R','S','T','U','V','W','X', // 2 	'Y','Z','a','b','c','d','e','f', // 3 	'g','h','i','j','k','l','m','n', // 4 	'o','p','q','r','s','t','u','v', // 5 	'w','x','y','z','0','1','2','3', // 6 	'4','5','6','7','8','9','+',','  // 7     };      private static final byte pem_convert_array[] = new byte[256];      static { 	for (int i = 0; i < 255; i++) 	    pem_convert_array[i] = -1; 	for(int i = 0; i < pem_array.length; i++) 	    pem_convert_array[pem_array[i]] = (byte) i;     }     }

user: user-2
content: current = (byte)(((a << 2) & 0xfc) | ((b >> 4) & 3));  这个怎么表示？用C语言  这个用c语言标识，也是这样的，没什么区别

user: user-3
content: 单纯那句, 看不出是什么语言. thinking in java 的预科, 直接 java 上手的程序员还是欠一些.

user: user-4
content: BYTE current = (BYTE)(((a << 2) & 0xfc) | ((b >>> 4) & 3));

user: user-5
content: 引用 8 楼 zhao4zhong1 的回复:#include <stdio.h> #include <string.h> #include <stdlib.h>  #define BASE64_VALUE_SZ 256 int     base64_value[BASE64_VALUE_SZ]; const unsigned char alphabet[65] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; class Base64Utility { public:     Base64Utility();     int base64_encode(char *src, int srclen, char *dst, int tail);     int base64_decode(char *src, int srclen, char *dst); private:     void base_64_init(void); }; Base64Utility::Base64Utility() {     base_64_init(); } void Base64Utility::base_64_init(void) {     int i;      for (i = 0; i < BASE64_VALUE_SZ; i++) base64_value[i] = -1;     for (i = 0; i < 64; i++) base64_value[(int) alphabet[i]] = i;     base64_value['='] = 0; } int Base64Utility::base64_encode(char *src, int srclen, char *dst, int tail) {     int     bits, char_count, len;     char    *o_char, *lim, *o_lim;     unsigned char   c;      if ( !src || !dst) return 0;     len = srclen;     lim = src + len;     o_char = dst;     o_lim  = dst + (len*4)/3 + 1;     char_count = 0;     bits = 0;     while ( (src < lim) && (o_char < o_lim)) {         c = *(src++);         bits += c;         char_count++;         if (char_count == 3) {             *(o_char++) = alphabet[bits >> 18];             *(o_char++) = alphabet[(bits >> 12) & 0x3f];             *(o_char++) = alphabet[(bits >> 6) & 0x3f];             *(o_char++) = alphabet[bits & 0x3f];             bits = 0;             char_count = 0;         } else {             bits <<= 8;         }     }     if (char_count != 0) {         bits <<= 16 - (8 * char_count);         *(o_char++) = alphabet[bits >> 18];         *(o_char++) = alphabet[(bits >> 12) & 0x3f];         if (char_count == 1) {             if (tail) {                 *(o_char++) = '=';                 *(o_char++) = '=';             }         } else {             *(o_char++) = alphabet[(bits >> 6) & 0x3f];             if (tail) {                 *(o_char++) = '=';             }         }     }     *(o_char) = 0;     return strlen(dst); } int Base64Utility::base64_decode(char *src, int srclen, char *dst) {     int j;     unsigned int k;     int c, base_result_sz;     long val;      if (!src || !dst) return 0;     base_result_sz = srclen;     val = c = 0;     for (j = 0; *src; src++) {         k = (int) *src % BASE64_VALUE_SZ;         if (base64_value[k] < 0) continue;         val <<= 6;         val += base64_value[k];         if (++c < 4) continue;         dst[j++] = (char) (val >> 16);         dst[j++] = (val >> 8) & 0xff;         dst[j++] = val & 0xff;         val = c = 0;     }     switch (c) {         case 2://xxxxxx xx0000             dst[j++] = (val >> 4) & 0xff;         break;         case 3://XXXXXX XXxxxx xxxx00             dst[j++] = (char) (val >> 10);             dst[j++] = (val >> 2) & 0xff;         break;     }     return j; } Base64Utility b64u; #define MAXLENS 1024768 #define MAXLEND 1366360 char bufd[MAXLEND]; char bufs[MAXLENS]; FILE *fs,*fd; int fsize; int main(int argc,char *argv[]) {     if (argc<4) {     USE:         printf("%s <-e|-E|-d> srcfile desfile\n",argv[0]);         return 1;     }     if (stricmp(argv[1],"-e") && stricmp(argv[1],"-d")) goto USE;     if (0==stricmp(argv[1],"-e")) {         fs=fopen(argv[2],"rb");         if (NULL==fs) {             printf("Can not open file %s!\n",argv[2]);             return 2;         }         fsize=fread(bufs,1,MAXLENS,fs);         if (fsize<=0) {             fclose(fs);             printf("Can not read file %s!\n",argv[2]);             return 3;         }         if (MAXLENS==fsize) printf("Warning: Up to %d bytes.\n",MAXLENS);         fclose(fs);         b64u.base64_encode(bufs,fsize,bufd,('E'==argv[2][1]));         fd=fopen(argv[3],"w");         if (NULL==fd) {             printf("Can not create file %s!\n",argv[3]);             return 4;         }         fprintf(fd,"%s",bufd);         fclose(fd);     } else {//0==stricmp(argv[1],"-d")         fd=fopen(argv[2],"rb");         if (NULL==fd) {             printf("Can not open file %s!\n",argv[2]);             return 2;         }         fsize=fread(bufd,1,MAXLEND,fd);         if (fsize<=0) {             fclose(fd);             printf("Can not read file %s!\n",argv[2]);             return 3;         }         if (MAXLEND==fsize) printf("Warning: Up to %d bytes.\n",MAXLEND);         fclose(fd);         fsize=b64u.base64_decode(bufd,fsize,bufs);         fs=fopen(argv[3],"wb");         if (NULL==fs) {             printf("Can not create file %s!\n",argv[3]);             return 4;         }         if (fsize!=(int)fwrite(bufs,1,fsize,fs)) {             printf("Write %s error!\n",argv[3]);             fclose(fs);             return 5;         }         fclose(fs);     }     return 0; }

user: user-0
content: 解不出来  编码是：&ZbBe+k4ATiplh072WTk-123123   int main(int argc,char *argv[]) {     memset(bufd,'\0',MAXLEND);     memset(bufs,'\0',MAXLENS);      strcpy(bufd,"&ZbBe+k4ATiplh072WTk-123123");     b64u.base64_decode(bufd,strlen(bufd),bufs);     printf("%s\n",bufs);   上面JAVA代码就可以

user: user-6
content: #include <stdio.h> #include <string.h> #include <stdlib.h>  #define BASE64_VALUE_SZ 256 int     base64_value[BASE64_VALUE_SZ]; const unsigned char alphabet[65] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; class Base64Utility { public:     Base64Utility();     int base64_encode(char *src, int srclen, char *dst, int tail);     int base64_decode(char *src, int srclen, char *dst); private:     void base_64_init(void); }; Base64Utility::Base64Utility() {     base_64_init(); } void Base64Utility::base_64_init(void) {     int i;      for (i = 0; i < BASE64_VALUE_SZ; i++) base64_value[i] = -1;     for (i = 0; i < 64; i++) base64_value[(int) alphabet[i]] = i;     base64_value['='] = 0; } int Base64Utility::base64_encode(char *src, int srclen, char *dst, int tail) {     int     bits, char_count, len;     char    *o_char, *lim, *o_lim;     unsigned char   c;      if ( !src || !dst) return 0;     len = srclen;     lim = src + len;     o_char = dst;     o_lim  = dst + (len*4)/3 + 1;     char_count = 0;     bits = 0;     while ( (src < lim) && (o_char < o_lim)) {         c = *(src++);         bits += c;         char_count++;         if (char_count == 3) {             *(o_char++) = alphabet[bits >> 18];             *(o_char++) = alphabet[(bits >> 12) & 0x3f];             *(o_char++) = alphabet[(bits >> 6) & 0x3f];             *(o_char++) = alphabet[bits & 0x3f];             bits = 0;             char_count = 0;         } else {             bits <<= 8;         }     }     if (char_count != 0) {         bits <<= 16 - (8 * char_count);         *(o_char++) = alphabet[bits >> 18];         *(o_char++) = alphabet[(bits >> 12) & 0x3f];         if (char_count == 1) {             if (tail) {                 *(o_char++) = '=';                 *(o_char++) = '=';             }         } else {             *(o_char++) = alphabet[(bits >> 6) & 0x3f];             if (tail) {                 *(o_char++) = '=';             }         }     }     *(o_char) = 0;     return strlen(dst); } int Base64Utility::base64_decode(char *src, int srclen, char *dst) {     int j;     unsigned int k;     int c, base_result_sz;     long val;      if (!src || !dst) return 0;     base_result_sz = srclen;     val = c = 0;     for (j = 0; *src; src++) {         k = (int) *src % BASE64_VALUE_SZ;         if (base64_value[k] < 0) continue;         val <<= 6;         val += base64_value[k];         if (++c < 4) continue;         dst[j++] = (char) (val >> 16);         dst[j++] = (val >> 8) & 0xff;         dst[j++] = val & 0xff;         val = c = 0;     }     switch (c) {         case 2://xxxxxx xx0000             dst[j++] = (val >> 4) & 0xff;         break;         case 3://XXXXXX XXxxxx xxxx00             dst[j++] = (char) (val >> 10);             dst[j++] = (val >> 2) & 0xff;         break;     }     return j; } Base64Utility b64u; #define MAXLENS 1024768 #define MAXLEND 1366360 char bufd[MAXLEND]; char bufs[MAXLENS]; FILE *fs,*fd; int fsize; int main(int argc,char *argv[]) {     if (argc<4) {     USE:         printf("%s <-e|-E|-d> srcfile desfile\n",argv[0]);         return 1;     }     if (stricmp(argv[1],"-e") && stricmp(argv[1],"-d")) goto USE;     if (0==stricmp(argv[1],"-e")) {         fs=fopen(argv[2],"rb");         if (NULL==fs) {             printf("Can not open file %s!\n",argv[2]);             return 2;         }         fsize=fread(bufs,1,MAXLENS,fs);         if (fsize<=0) {             fclose(fs);             printf("Can not read file %s!\n",argv[2]);             return 3;         }         if (MAXLENS==fsize) printf("Warning: Up to %d bytes.\n",MAXLENS);         fclose(fs);         b64u.base64_encode(bufs,fsize,bufd,('E'==argv[2][1]));         fd=fopen(argv[3],"w");         if (NULL==fd) {             printf("Can not create file %s!\n",argv[3]);             return 4;         }         fprintf(fd,"%s",bufd);         fclose(fd);     } else {//0==stricmp(argv[1],"-d")         fd=fopen(argv[2],"rb");         if (NULL==fd) {             printf("Can not open file %s!\n",argv[2]);             return 2;         }         fsize=fread(bufd,1,MAXLEND,fd);         if (fsize<=0) {             fclose(fd);             printf("Can not read file %s!\n",argv[2]);             return 3;         }         if (MAXLEND==fsize) printf("Warning: Up to %d bytes.\n",MAXLEND);         fclose(fd);         fsize=b64u.base64_decode(bufd,fsize,bufs);         fs=fopen(argv[3],"wb");         if (NULL==fs) {             printf("Can not create file %s!\n",argv[3]);             return 4;         }         if (fsize!=(int)fwrite(bufs,1,fsize,fs)) {             printf("Write %s error!\n",argv[3]);             fclose(fs);             return 5;         }         fclose(fs);     }     return 0; }

user: user-0
content: 有没有谁知道怎么把上面的代码转成C或者C++  或者有没有人有IMAP协议的 BASE64 解码代码？

user: user-7
content: C语言没有>>> C语言只保证unsigned是逻辑右移，对于signed要看编译器实现了。

user: user-0
content: C语言没法表示 >>>  编译出错  expected primary-expression before ‘>’ token