Skip to content

Instantly share code, notes, and snippets.

@turbidsoul
Created March 23, 2013 08:28
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save turbidsoul/5226998 to your computer and use it in GitHub Desktop.
Save turbidsoul/5226998 to your computer and use it in GitHub Desktop.
java base 16 编码和解码
/***
* Copyright 2006 bsmith@qq.com
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a
* copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations
* under the License.
*/
package cn.is.ftp;
import java.io.UnsupportedEncodingException;
/**
* base 16 hex encoding.
*
* @author bsmith.zhao
* @date 2006-05-16 11:22:12
*/
public class Base16 {
/**
* the example.
*
* @param args
* @throws UnsupportedEncodingException
*/
public static void main(String args[]) throws UnsupportedEncodingException {
String str = "0123456abcdef中文123abc";
// byte[] data = str.getBytes("UTF-8");
byte[] data = str.getBytes("UTF-8");
String hex = encode(data, 0, data.length);
System.out.println(hex);
data = decode(hex);
str = new String(data, "UTF-8");
System.out.println(str);
hex += "a";
data = decode(hex);
str = new String(data, "UTF-8");
System.out.println(str);
}
// encoding characters table.
public static final char[] ENC_TAB = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
// decoding characters table.
public static final byte[] DEC_TAB = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 16
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 32
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 48
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 64
0x00, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 80
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 96
0x00, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 112
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
/**
* encode byte array data to base 16 hex string.
*
* @param data byte array data.
* @return base 16 hex string.
*/
public static String encode(byte[] data) {
return encode(data, 0, data.length);
}
/**
* encode byte array data from offset to offset+length to base 16 hex string.
*
* @param data byte array data.
* @param offset start index, included.
* @param length total encode length.
* @return the base 16 hex string.
*/
public static String encode(byte[] data, int offset, int length) {
StringBuffer buff = new StringBuffer(length * 2);
int i = offset, total = offset + length;
while (i < total) {
buff.append(ENC_TAB[(data[i] & 0xF0) >> 4]);
buff.append(ENC_TAB[data[i] & 0x0F]);
i++;
}
return buff.toString();
}
/**
* decode base 16 hex string to byte array.
*
* @param hex base 16 hex string.
* @return byte array data.
*/
public static byte[] decode(String hex) {
byte[] data = new byte[hex.length() / 2];
decode(hex, data, 0);
return data;
}
/**
* decode base 16 hex string to byte array.
*
* @param hex base 16 hex string.
* @param data byte array data.
* @param offset byte array data start index, included.
*/
public static void decode(String hex, byte[] data, int offset) {
int i = 0, total = (hex.length() / 2) * 2, idx = offset;
while (i < total) {
data[idx++] = (byte) ((DEC_TAB[hex.charAt(i++)] << 4) | DEC_TAB[hex.charAt(i++)]);
}
}
}
/***
* Copyright 2006 bsmith@qq.com
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a
* copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations
* under the License.
*/
package cn.is.ftp;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
/**
* base 64 hex string encoding.
*
* @date 2006-05-16 11:22:12
*/
public class Base64 {
/**
* the example.
*
* @param args
* @throws UnsupportedEncodingException
*/
public static void main(String[] args) throws UnsupportedEncodingException {
String data = "0123456abcdef中文123abc";
byte[] result = Base64.encode(data.getBytes());
String str = "+/\n\r";
System.out.println(URLEncoder.encode(str, "UTF-8"));
System.out.println(data);
System.out.println(new String(result));
System.out.println(new String(Base64.decode(new String(result))));
}
/**
* encode characters table.
*/
private static final byte[] ENC_TAB = { (byte) 'A', (byte) 'B', (byte) 'C', (byte) 'D', (byte) 'E', (byte) 'F', (byte) 'G', (byte) 'H',
(byte) 'I', (byte) 'J', (byte) 'K', (byte) 'L', (byte) 'M', (byte) 'N', (byte) 'O', (byte) 'P', (byte) 'Q', (byte) 'R', (byte) 'S',
(byte) 'T', (byte) 'U', (byte) 'V', (byte) 'W', (byte) 'X', (byte) 'Y', (byte) 'Z', (byte) 'a', (byte) 'b', (byte) 'c', (byte) 'd',
(byte) 'e', (byte) 'f', (byte) 'g', (byte) 'h', (byte) 'i', (byte) 'j', (byte) 'k', (byte) 'l', (byte) 'm', (byte) 'n', (byte) 'o',
(byte) 'p', (byte) 'q', (byte) 'r', (byte) 's', (byte) 't', (byte) 'u', (byte) 'v', (byte) 'w', (byte) 'x', (byte) 'y', (byte) 'z',
(byte) '0', (byte) '1', (byte) '2', (byte) '3', (byte) '4', (byte) '5', (byte) '6', (byte) '7', (byte) '8', (byte) '9', (byte) '+',
(byte) '/' };
/**
* decode characters table.
*/
private static final byte[] DEC_TAB;
static {
DEC_TAB = new byte[128];
for (int i = 0; i < 128; i++) {
DEC_TAB[i] = (byte) -1;
}
for (int i = 'A'; i <= 'Z'; i++) {
DEC_TAB[i] = (byte) (i - 'A');
}
for (int i = 'a'; i <= 'z'; i++) {
DEC_TAB[i] = (byte) (i - 'a' + 26);
}
for (int i = '0'; i <= '9'; i++) {
DEC_TAB[i] = (byte) (i - '0' + 52);
}
DEC_TAB['+'] = 62;
DEC_TAB['/'] = 63;
}
/**
* encode byte array data to base 64 hex string data.
*
* @param data byte array data.
* @return base 64 hex string data. use new String("UTF-8") to get the String format.
*/
public static byte[] encode(byte[] data) {
byte[] bytes;
int modulus = data.length % 3;
if (modulus == 0) {
bytes = new byte[(4 * data.length) / 3];
} else {
bytes = new byte[4 * ((data.length / 3) + 1)];
}
int dataLength = (data.length - modulus);
int a1;
int a2;
int a3;
for (int i = 0, j = 0; i < dataLength; i += 3, j += 4) {
a1 = data[i] & 0xff;
a2 = data[i + 1] & 0xff;
a3 = data[i + 2] & 0xff;
bytes[j] = ENC_TAB[(a1 >>> 2) & 0x3f];
bytes[j + 1] = ENC_TAB[((a1 << 4) | (a2 >>> 4)) & 0x3f];
bytes[j + 2] = ENC_TAB[((a2 << 2) | (a3 >>> 6)) & 0x3f];
bytes[j + 3] = ENC_TAB[a3 & 0x3f];
}
int b1;
int b2;
int b3;
int d1;
int d2;
switch (modulus) {
case 0: /* nothing left to do */
break;
case 1:
d1 = data[data.length - 1] & 0xff;
b1 = (d1 >>> 2) & 0x3f;
b2 = (d1 << 4) & 0x3f;
bytes[bytes.length - 4] = ENC_TAB[b1];
bytes[bytes.length - 3] = ENC_TAB[b2];
bytes[bytes.length - 2] = (byte) '=';
bytes[bytes.length - 1] = (byte) '=';
break;
case 2:
d1 = data[data.length - 2] & 0xff;
d2 = data[data.length - 1] & 0xff;
b1 = (d1 >>> 2) & 0x3f;
b2 = ((d1 << 4) | (d2 >>> 4)) & 0x3f;
b3 = (d2 << 2) & 0x3f;
bytes[bytes.length - 4] = ENC_TAB[b1];
bytes[bytes.length - 3] = ENC_TAB[b2];
bytes[bytes.length - 2] = ENC_TAB[b3];
bytes[bytes.length - 1] = (byte) '=';
break;
}
return bytes;
}
/**
* decode base 64 string data to byte data array.
*
* @param data base 64 string data.
* @return byte data array.
*/
public static byte[] decode(byte[] data) {
byte[] bytes;
byte b1;
byte b2;
byte b3;
byte b4;
data = discardNonBase64Bytes(data);
if (data[data.length - 2] == '=') {
bytes = new byte[(((data.length / 4) - 1) * 3) + 1];
} else if (data[data.length - 1] == '=') {
bytes = new byte[(((data.length / 4) - 1) * 3) + 2];
} else {
bytes = new byte[((data.length / 4) * 3)];
}
for (int i = 0, j = 0; i < (data.length - 4); i += 4, j += 3) {
b1 = DEC_TAB[data[i]];
b2 = DEC_TAB[data[i + 1]];
b3 = DEC_TAB[data[i + 2]];
b4 = DEC_TAB[data[i + 3]];
bytes[j] = (byte) ((b1 << 2) | (b2 >> 4));
bytes[j + 1] = (byte) ((b2 << 4) | (b3 >> 2));
bytes[j + 2] = (byte) ((b3 << 6) | b4);
}
if (data[data.length - 2] == '=') {
b1 = DEC_TAB[data[data.length - 4]];
b2 = DEC_TAB[data[data.length - 3]];
bytes[bytes.length - 1] = (byte) ((b1 << 2) | (b2 >> 4));
} else if (data[data.length - 1] == '=') {
b1 = DEC_TAB[data[data.length - 4]];
b2 = DEC_TAB[data[data.length - 3]];
b3 = DEC_TAB[data[data.length - 2]];
bytes[bytes.length - 2] = (byte) ((b1 << 2) | (b2 >> 4));
bytes[bytes.length - 1] = (byte) ((b2 << 4) | (b3 >> 2));
} else {
b1 = DEC_TAB[data[data.length - 4]];
b2 = DEC_TAB[data[data.length - 3]];
b3 = DEC_TAB[data[data.length - 2]];
b4 = DEC_TAB[data[data.length - 1]];
bytes[bytes.length - 3] = (byte) ((b1 << 2) | (b2 >> 4));
bytes[bytes.length - 2] = (byte) ((b2 << 4) | (b3 >> 2));
bytes[bytes.length - 1] = (byte) ((b3 << 6) | b4);
}
return bytes;
}
/**
* decode base 64 string to byte array data.
*
* @param data base 64 string.
* @return byte array data.
*/
public static byte[] decode(String data) {
byte[] bytes;
byte b1;
byte b2;
byte b3;
byte b4;
data = discardNonBase64Chars(data);
if (data.charAt(data.length() - 2) == '=') {
bytes = new byte[(((data.length() / 4) - 1) * 3) + 1];
} else if (data.charAt(data.length() - 1) == '=') {
bytes = new byte[(((data.length() / 4) - 1) * 3) + 2];
} else {
bytes = new byte[((data.length() / 4) * 3)];
}
for (int i = 0, j = 0; i < (data.length() - 4); i += 4, j += 3) {
b1 = DEC_TAB[data.charAt(i)];
b2 = DEC_TAB[data.charAt(i + 1)];
b3 = DEC_TAB[data.charAt(i + 2)];
b4 = DEC_TAB[data.charAt(i + 3)];
bytes[j] = (byte) ((b1 << 2) | (b2 >> 4));
bytes[j + 1] = (byte) ((b2 << 4) | (b3 >> 2));
bytes[j + 2] = (byte) ((b3 << 6) | b4);
}
if (data.charAt(data.length() - 2) == '=') {
b1 = DEC_TAB[data.charAt(data.length() - 4)];
b2 = DEC_TAB[data.charAt(data.length() - 3)];
bytes[bytes.length - 1] = (byte) ((b1 << 2) | (b2 >> 4));
} else if (data.charAt(data.length() - 1) == '=') {
b1 = DEC_TAB[data.charAt(data.length() - 4)];
b2 = DEC_TAB[data.charAt(data.length() - 3)];
b3 = DEC_TAB[data.charAt(data.length() - 2)];
bytes[bytes.length - 2] = (byte) ((b1 << 2) | (b2 >> 4));
bytes[bytes.length - 1] = (byte) ((b2 << 4) | (b3 >> 2));
} else {
b1 = DEC_TAB[data.charAt(data.length() - 4)];
b2 = DEC_TAB[data.charAt(data.length() - 3)];
b3 = DEC_TAB[data.charAt(data.length() - 2)];
b4 = DEC_TAB[data.charAt(data.length() - 1)];
bytes[bytes.length - 3] = (byte) ((b1 << 2) | (b2 >> 4));
bytes[bytes.length - 2] = (byte) ((b2 << 4) | (b3 >> 2));
bytes[bytes.length - 1] = (byte) ((b3 << 6) | b4);
}
return bytes;
}
private static byte[] discardNonBase64Bytes(byte[] data) {
byte[] temp = new byte[data.length];
int bytesCopied = 0;
for (int i = 0; i < data.length; i++) {
if (isValidBase64Byte(data[i])) {
temp[bytesCopied++] = data[i];
}
}
byte[] newData = new byte[bytesCopied];
System.arraycopy(temp, 0, newData, 0, bytesCopied);
return newData;
}
private static String discardNonBase64Chars(String data) {
StringBuffer sb = new StringBuffer();
int length = data.length();
for (int i = 0; i < length; i++) {
if (isValidBase64Byte((byte) (data.charAt(i)))) {
sb.append(data.charAt(i));
}
}
return sb.toString();
}
private static boolean isValidBase64Byte(byte b) {
if (b == '=') {
return true;
} else if ((b < 0) || (b >= 128)) {
return false;
} else if (DEC_TAB[b] == -1) {
return false;
}
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment