1 19 package gcc.util; 20 21 public class Base64Binary 22 { 23 26 public static String toString(byte[] bytes) 27 { 28 return toString(bytes, 0, bytes.length); 29 } 30 31 public static String toString(byte[] bytes, int offset, int length) 32 { 33 StringBuffer s = new StringBuffer((length * 4) / 3 + 1); 34 int n = offset + length; 35 for (int i = offset; i < n; i += 3) 36 { 37 44 int value; 45 int chars; 46 if (i < n - 2) 47 { 48 value = (0x00FF0000 & (bytes[i] << 16)) 49 | (0x0000FF00 & (bytes[i + 1] << 8)) 50 | (0x000000FF & bytes[i + 2]); 51 chars = 4; 52 } 53 else if (i < n - 1) 54 { 55 value = (0x00FF0000 & (bytes[i] << 16)) 56 | (0x0000FF00 & (bytes[i + 1] << 8)); 57 chars = 3; 58 } 59 else 60 { 61 value = (0x00FF0000 & (bytes[i] << 16)); 62 chars = 2; 63 } 64 while (chars-- > 0) 65 { 66 int x = (0x00FC0000 & value) >> 18; 67 char c = getChar(x); 68 s.append(c); 69 value = value << 6; 70 } 71 if (i == n - 1) 72 { 73 s.append("=="); 74 } 75 else if (i == n - 2) 76 { 77 s.append('='); 78 } 79 } 80 return s.toString(); 81 } 82 83 private static char getChar(int c) 84 { 85 if (c < 26) 86 { 87 return (char)('A' + c); 88 } 89 else if (c < 52) 90 { 91 return (char)('a' + (c - 26)); 92 } 93 else if (c < 62) 94 { 95 return (char)('0' + (c - 52)); 96 } 97 else if (c == 62) 98 { 99 return '+'; 100 } 101 else { 103 return '/'; 104 } 105 } 106 } 107 | Popular Tags |