182 lines
4.6 KiB
Java
182 lines
4.6 KiB
Java
package com.amarsoft.aims.util;
|
||
|
||
import java.io.ByteArrayOutputStream;
|
||
import java.io.UnsupportedEncodingException;
|
||
|
||
import com.amarsoft.are.lang.StringX;
|
||
|
||
/**
|
||
* 十六进制辅助处理类
|
||
* @author EX-YANGSONG001
|
||
*
|
||
*/
|
||
public class HexHelper {
|
||
|
||
/**
|
||
* unicode转字串
|
||
* @param str
|
||
* @return
|
||
*/
|
||
public static String unicode2String(String... str){
|
||
StringBuffer sb = new StringBuffer();
|
||
for(String s:str){
|
||
if(StringX.isEmpty(s))continue;
|
||
int a = Integer.parseInt(s,16);
|
||
sb.append((char)a);
|
||
}
|
||
return sb.toString();
|
||
}
|
||
/**
|
||
* 转化字符串为十六进制编码
|
||
* @param s
|
||
* @return
|
||
*/
|
||
public static String toHexString(String s) {
|
||
String str = "";
|
||
for (int i = 0; i < s.length(); i++) {
|
||
int ch = (int) s.charAt(i);
|
||
String s4 = Integer.toHexString(ch);
|
||
str = str + s4;
|
||
}
|
||
return str;
|
||
}
|
||
|
||
// 转化十六进制编码为字符串
|
||
public static String toStringHex1(String s,String charset) throws UnsupportedEncodingException {
|
||
byte[] baKeyword = new byte[s.length() / 2];
|
||
for (int i = 0; i < baKeyword.length; i++) {
|
||
baKeyword[i] = (byte) (0xff & Integer.parseInt(s.substring(i * 2, i * 2 + 2), 16));
|
||
}
|
||
s = new String(baKeyword, charset);// UTF-16le:Not
|
||
return s;
|
||
}
|
||
|
||
public static void main(String[] args) {
|
||
String s = encode("[投资计划规模]*85%+[人行基准利率(5年以上)]*90%");
|
||
System.out.println(s);
|
||
System.out.println(decode("GB18030","5B62958D448BA1521289C46A215D2A3835252B5B4EBA884C57FA51C65229738728355E744EE54E0A295D2A393025"));
|
||
}
|
||
|
||
/*
|
||
* 16进制数字字符集
|
||
*/
|
||
private static String hexString = "0123456789ABCDEF";
|
||
|
||
/**
|
||
* 将字符串编码成16进制数字,适用于所有字符(包括中文)
|
||
* @param str
|
||
* @return
|
||
*/
|
||
public static String encode(String str) {
|
||
// 根据默认编码获取字节数组
|
||
byte[] bytes = str.getBytes();
|
||
StringBuilder sb = new StringBuilder(bytes.length * 2);
|
||
// 将字节数组中每个字节拆解成2位16进制整数
|
||
for (int i = 0; i < bytes.length; i++) {
|
||
sb.append(hexString.charAt((bytes[i] & 0xf0) >> 4));
|
||
sb.append(hexString.charAt((bytes[i] & 0x0f) >> 0));
|
||
}
|
||
return sb.toString();
|
||
}
|
||
|
||
|
||
/**
|
||
* 将16进制数字解码成字符串,适用于所有字符(包括中文)
|
||
* @param charset
|
||
* @param str
|
||
* @return
|
||
* @throws UnsupportedEncodingException
|
||
*/
|
||
public static String decode(String charset,String[] str) throws UnsupportedEncodingException {
|
||
StringBuffer sb = new StringBuffer();
|
||
for(String s:str){
|
||
ByteArrayOutputStream baos = new ByteArrayOutputStream(s.length() / 2);
|
||
// 将每2位16进制整数组装成一个字节
|
||
for (int i = 0; i < s.length(); i += 2){
|
||
baos.write((hexString.indexOf(s.charAt(i)) << 4 | hexString.indexOf(s.charAt(i + 1))));
|
||
}
|
||
sb.append(new String(baos.toByteArray(),charset));
|
||
}
|
||
return sb.toString();
|
||
}
|
||
/**
|
||
* 将16进制数字解码成字符串,适用于所有字符(包括中文)
|
||
* @param bytes
|
||
* @return
|
||
*/
|
||
public static String decode(String... str) {
|
||
StringBuffer sb = new StringBuffer();
|
||
for(String s:str){
|
||
ByteArrayOutputStream baos = new ByteArrayOutputStream(s.length() / 2);
|
||
// 将每2位16进制整数组装成一个字节
|
||
for (int i = 0; i < s.length(); i += 2){
|
||
baos.write((hexString.indexOf(s.charAt(i)) << 4 | hexString.indexOf(s.charAt(i + 1))));
|
||
}
|
||
sb.append(new String(baos.toByteArray()));
|
||
}
|
||
return sb.toString();
|
||
}
|
||
|
||
/**
|
||
* 将指定byte数组以16进制的形式打印到控制台
|
||
* @param hint
|
||
* @param b
|
||
*/
|
||
public static void printHexString(String hint, byte[] b) {
|
||
System.out.print(hint);
|
||
for (int i = 0; i < b.length; i++) {
|
||
String hex = Integer.toHexString(b[i] & 0xFF);
|
||
if (hex.length() == 1) {
|
||
hex = '0' + hex;
|
||
}
|
||
System.out.print(hex.toUpperCase() + " ");
|
||
}
|
||
System.out.println("");
|
||
}
|
||
|
||
/**
|
||
* 二进制转为16进制
|
||
* @param b
|
||
* @return
|
||
*/
|
||
public static String bytes2HexString(byte[] b) {
|
||
String ret = "";
|
||
for (int i = 0; i < b.length; i++) {
|
||
String hex = Integer.toHexString(b[i] & 0xFF);
|
||
if (hex.length() == 1) {
|
||
hex = '0' + hex;
|
||
}
|
||
ret += hex.toUpperCase();
|
||
}
|
||
return ret;
|
||
}
|
||
|
||
/**
|
||
* 将两个ASCII字符合成一个字节; 如:"EF"--> 0xEF
|
||
* @param src0
|
||
* @param src1
|
||
* @return
|
||
*/
|
||
public static byte uniteBytes(byte src0, byte src1) {
|
||
byte _b0 = Byte.decode("0x" + new String(new byte[] { src0 })).byteValue();
|
||
_b0 = (byte) (_b0 << 4);
|
||
byte _b1 = Byte.decode("0x" + new String(new byte[] { src1 })).byteValue();
|
||
byte ret = (byte) (_b0 ^ _b1);
|
||
return ret;
|
||
}
|
||
|
||
/**
|
||
* 将指定字符串src,以每两个字符分割转换为16进制形式 如:"2B44EFD9" --> byte[]{0x2B, 0x44, 0xEF,0xD9}
|
||
* @param src
|
||
* @return
|
||
*/
|
||
public static byte[] hexString2Bytes(String src) {
|
||
byte[] ret = new byte[8];
|
||
byte[] tmp = src.getBytes();
|
||
for (int i = 0; i < 8; i++) {
|
||
ret[i] = uniteBytes(tmp[i * 2], tmp[i * 2 + 1]);
|
||
}
|
||
return ret;
|
||
}
|
||
}
|