String字符串和Unicode字符相互转换
package util;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* String 与 Unicode 互相转换的工具类
*/
public class StringUnicodeUtil {
/**
* 含有unicode 的字符串转一般字符串
* @param unicodeStr 混有 Unicode 的字符串
*/
public static String unicodeStr2String(String unicodeStr) {
int length = unicodeStr.length();
int count = 0;
//正则匹配条件,可匹配“\\u”1到4位,一般是4位可直接使用 String regex = "\\\\u[a-f0-9A-F]{4}";
String regex = "\\\\u[a-f0-9A-F]{1,4}";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(unicodeStr);
StringBuffer sb = new StringBuffer();
while(matcher.find()) {
String oldChar = matcher.group();//原本的Unicode字符
String newChar = unicode2String(oldChar);//转换为普通字符
// int index = unicodeStr.indexOf(oldChar);
// 在遇见重复出现的unicode代码的时候会造成从源字符串获取非unicode编码字符的时候截取索引越界等
int index = matcher.start();
sb.append(unicodeStr.substring(count, index));//添加前面不是unicode的字符
sb.append(newChar);//添加转换后的字符
count = index+oldChar.length();//统计下标移动的位置
}
sb.append(unicodeStr.substring(count, length));//添加末尾不是Unicode的字符
return sb.toString();
}
/**
* 字符串转换unicode
* @param string
*/
public static String string2Unicode(String string) {
StringBuffer unicode = new StringBuffer();
for (int i = 0; i < string.length(); i++) {
// 取出每一个字符
char c = string.charAt(i);
// 转换为unicode
unicode.append("\\u" + Integer.toHexString(c));
}
return unicode.toString();
}
/**
* unicode 转字符串
* @param unicode 全为 Unicode 的字符串
*/
public static String unicode2String(String unicode) {
StringBuffer string = new StringBuffer();
String[] hex = unicode.split("\\\\u");
for (int i = 1; i < hex.length; i++) {
// 转换出每一个代码点
int data = Integer.parseInt(hex[i], 16);
// 追加成string
string.append((char) data);
}
return string.toString();
}
public static void main(String[] args) {
String str = "abc";
String str2 = string2Unicode(str);
System.out.println(str2);
System.out.println(unicodeStr2String(str2));
System.out.println(unicodeStr2String("\\u61HJ\\u62\\u63(sfkfdsl)"));
}
}
正则匹配规则
String regex = "\\\\u[a-f0-9A-F]{1,4}";
URL的转码与解码(String字符串)
转码:
java.net.URLEncoder.encode(String s);
解码:
java.net.URLDecoder.decode(String s);
Base64的转码与解码
直接使用工具类的方法
import org.apache.commons.codec.binary.Base64;
import sun.misc.BASE64Encoder;
import java.io.*;
import java.math.BigInteger;
import java.net.URLEncoder;
import java.util.zip.Deflater;
import java.util.zip.Inflater;
import java.util.zip.InflaterOutputStream;
/**
* Base64工具(自实现)<br/>
* 可使用Android自带的Base64编码解码
*/
public class Base64Util {
private static final byte[] encodingTable = {(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) '/'};
private static final byte[] decodingTable;
static {
decodingTable = new byte[128];
for (int i = 0; i < 128; i++) {
decodingTable[i] = (byte) -1;
}
for (int i = 'A'; i <= 'Z'; i++) {
decodingTable[i] = (byte) (i - 'A');
}
for (int i = 'a'; i <= 'z'; i++) {
decodingTable[i] = (byte) (i - 'a' + 26);
}
for (int i = '0'; i <= '9'; i++) {
decodingTable[i] = (byte) (i - '0' + 52);
}
decodingTable['+'] = 62;
decodingTable['/'] = 63;
}
@SuppressWarnings("restriction")
public static String base64EncodeString(byte[] input) {
String str = null;
try {
if ((input != null) && (input.length > 0)) {
BASE64Encoder encoder = new BASE64Encoder();
str = encoder.encode(input);
}
} catch (Exception ex) {
ex.printStackTrace();
}
return str;
}
/**
* 编码为String
* @param data
* @return
*/
public static String encodeToString(byte[] data) {
try {
return new String(encode(data), "utf-8");
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}
/**
* 编码为Url专用的String, 用于Http的Url参数, 否则+号会变空格
* @param data
* @return
*/
public static String encodeToUrlEncodedString(byte[] data){
try {
return URLEncoder.encode(encodeToString(data), "UTF-8");
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}
/**
* 编码
* @param data
* @return
*/
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] = encodingTable[(a1 >>> 2) & 0x3f];
bytes[j + 1] = encodingTable[((a1 << 4) | (a2 >>> 4)) & 0x3f];
bytes[j + 2] = encodingTable[((a2 << 2) | (a3 >>> 6)) & 0x3f];
bytes[j + 3] = encodingTable[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] = encodingTable[b1];
bytes[bytes.length - 3] = encodingTable[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] = encodingTable[b1];
bytes[bytes.length - 3] = encodingTable[b2];
bytes[bytes.length - 2] = encodingTable[b3];
bytes[bytes.length - 1] = (byte) '=';
break;
}
return bytes;
}
/**
* 解码
* @param data
* @return
*/
public static byte[] decode(byte[] data) {
byte[] bytes;
byte b1;
byte b2;
byte b3;
byte b4;
data = discardNonBase64Bytes(data);
if(data.length==0){
throw new RuntimeException("字节数组为空");
}
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 = decodingTable[data[i]];
b2 = decodingTable[data[i + 1]];
b3 = decodingTable[data[i + 2]];
b4 = decodingTable[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 = decodingTable[data[data.length - 4]];
b2 = decodingTable[data[data.length - 3]];
bytes[bytes.length - 1] = (byte) ((b1 << 2) | (b2 >> 4));
} else if (data[data.length - 1] == '=') {
b1 = decodingTable[data[data.length - 4]];
b2 = decodingTable[data[data.length - 3]];
b3 = decodingTable[data[data.length - 2]];
bytes[bytes.length - 2] = (byte) ((b1 << 2) | (b2 >> 4));
bytes[bytes.length - 1] = (byte) ((b2 << 4) | (b3 >> 2));
} else {
b1 = decodingTable[data[data.length - 4]];
b2 = decodingTable[data[data.length - 3]];
b3 = decodingTable[data[data.length - 2]];
b4 = decodingTable[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;
}
/**
* 解码
* @param data
* @return
*/
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 = decodingTable[data.charAt(i)];
b2 = decodingTable[data.charAt(i + 1)];
b3 = decodingTable[data.charAt(i + 2)];
b4 = decodingTable[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 = decodingTable[data.charAt(data.length() - 4)];
b2 = decodingTable[data.charAt(data.length() - 3)];
bytes[bytes.length - 1] = (byte) ((b1 << 2) | (b2 >> 4));
} else if (data.charAt(data.length() - 1) == '=') {
b1 = decodingTable[data.charAt(data.length() - 4)];
b2 = decodingTable[data.charAt(data.length() - 3)];
b3 = decodingTable[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 = decodingTable[data.charAt(data.length() - 4)];
b2 = decodingTable[data.charAt(data.length() - 3)];
b3 = decodingTable[data.charAt(data.length() - 2)];
b4 = decodingTable[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 (decodingTable[b] == -1) {
return false;
}
return true;
}
/**
* 压缩
* @param input
* @return
*/
public static byte[] compressBytes(byte input[]) {
int cachesize = 1024;
Deflater compresser = new Deflater();
compresser.reset();
compresser.setInput(input);
compresser.finish();
byte output[] = new byte[0];
ByteArrayOutputStream o = new ByteArrayOutputStream(input.length);
try {
byte[] buf = new byte[cachesize];
int got;
while (!compresser.finished()) {
got = compresser.deflate(buf);
o.write(buf, 0, got);
}
output = o.toByteArray();
} finally {
try {
o.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return output;
}
/**
* 解压缩
* @param input
* @return
*/
public static byte[] decompressBytes(byte input[]) {
int cachesize = 1024;
Inflater decompresser = new Inflater();
byte output[] = new byte[0];
decompresser.reset();
decompresser.setInput(input);
ByteArrayOutputStream o = new ByteArrayOutputStream(input.length);
try {
byte[] buf = new byte[cachesize];
int got;
while (!decompresser.finished()) {
got = decompresser.inflate(buf);
o.write(buf, 0, got);
}
output = o.toByteArray();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
o.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return output;
}
/**
* 解压缩String
* @param encdata
* @return
*/
public static String decompressData(String encdata) {
try {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
InflaterOutputStream zos = new InflaterOutputStream(bos);
zos.write(Base64Util.decode(encdata));
zos.close();
return new String(bos.toByteArray());
} catch (Exception ex) {
ex.printStackTrace();
return "UNZIP_ERR";
}
}
/**
* 对json字符串进行base64解码
* @param json
* @return
*/
public static String base64Decode(String json){
try {
return new String(Base64Util.decode(json.getBytes("UTF-8")), "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return "";
}
}
/**
* 本地图片转换成base64字符串
* @param imgFile 图片本地路径
* @return
*
* @author ZHANGJL
* @dateTime 2018-02-23 14:40:46
*/
public static String ImageToBase64ByLocal(String imgFile) {// 将图片文件转化为字节数组字符串,并对其进行Base64编码处理
InputStream in = null;
byte[] data = null;
// 读取图片字节数组
try {
in = new FileInputStream(imgFile);
data = new byte[in.available()];
in.read(data);
in.close();
} catch (IOException e) {
e.printStackTrace();
}
// 对字节数组Base64编码
BASE64Encoder encoder = new BASE64Encoder();
return encoder.encode(data);// 返回Base64编码过的字节数组字符串
}
/**
* base64编码 String → String
* 依赖于commons.codec.jar包
* @param input
* @return
*/
public static String base64EncodeStrToStr(String input){
String output = null;
try {
byte[] temp = Base64.encodeBase64(input.getBytes("utf-8"));
output = new String(temp, "utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return output;
}
/**
* 大数字转换字节流(字节数组)型数据
*
* @param num bigInteger
*/
public static byte[] byteConvert32Bytes(BigInteger num)
{
byte temp[];
if(num == null)
{
return null;
}
if(num.toByteArray().length == 33)
{
temp = new byte[32];
System.arraycopy(num.toByteArray(), 1, temp, 0, 32);
}
else if(num.toByteArray().length == 32)
{
temp = num.toByteArray();
}
else
{
temp = new byte[32];
for(int i = 0; i < 32 - num.toByteArray().length; i++)
{
temp[i] = 0;
}
System.arraycopy(num.toByteArray(), 0, temp, 32 - num.toByteArray().length, num.toByteArray().length);
}
return temp;
}
/**
* 解密加密后的字符串
* @param data
* @return
*/
public static String encodeStr(String data) {
try {
if (null == data) {
return null;
}
return new String(Base64Util.decode(data.getBytes("UTF-8")), "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return null;
}
}
校验字符串是否有编码:
public static boolean isBase64(String str) {
String base64Pattern = "^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)$";
return Pattern.matches(base64Pattern, str);
}
解码与编码的方法:
String data = Base64Util.encode(byte[]); byte[] bytes = Base64Util.decode(data);
Byte[]和String之间转换
//String转byte[]
byte[] bytes = string.getBytes();
//byte[]转String
String s = new String(bytes);
后面还有一些base64什么的转换 敲代码去了 过会儿补