package com.e.util;
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
/**
* 编码处理类
*
* @author sky
*/
public class Charset
{
private static String HEX = "0123456789ABCDEF";
private static byte[] _HexToBytes(String hex)
{
String _hex = hex.toUpperCase();
byte[] result = new byte[hex.length() / 2];
for (int i = 0; i < _hex.length() / 2; i++)
{
byte HH = (byte) (HEX.indexOf(_hex.substring(2 * i, 2 * i + 1)) << 4);
byte HL = (byte) (HEX.indexOf(_hex.substring(2 * i + 1, 2 * i + 2)));
result[i] = (byte) (HH | HL);
}
return result;
}
public static String toHex(byte one)
{
String HEX = "0123456789ABCDEF";
byte[] bTmp = new byte[2];
bTmp[0] = (byte) (HEX.charAt((one & 0xf0) >> 4));
bTmp[1] = (byte) (HEX.charAt(one & 0x0f));
String result = new String(bTmp);
return result;
}
/**
* 将字符串转为十六进制,BLOB/CLOB时常用
*
* @param value
* 字符串
* @return 16进制字符串
*/
public static String StrToHex(String value)
{
if (value == null)
{
return "";
}
String str = "";
byte[] btmp = value.getBytes();
for (int i = 0; i < btmp.length; i++)
{
str = str + toHex(btmp[i]);
}
return str;
}
/**
* 将十六进制改为字符串
*
* @param hex
* 16进制字符串
* @return 字符串
*/
public static String HexToStr(String hex)
{
if (hex == null)
{
return "";
}
byte temp[] = _HexToBytes(hex);
String result = new String(temp);
return result;
}
/**
* 将十六进制字符串改为字节数组
*
* @param hex
* 16进制字符串
* @return 字节数组
*/
public static byte[] hexToBytes(String hex)
{
byte temp[] = _HexToBytes(hex);
return temp;
}
/**
* 将字节数组改为16进制字符串
*
* @param bytes
* 字节数组
* @return 16进制字符串
*/
public static String bytesToHex(byte[] bytes)
{
String ret = "";
for (int i = 0; i < bytes.length; i++)
{
ret += toHex(bytes[i]);
}
return ret;
}
/**
* 将字符串编码改为Unicode
*
* @param value
* 字符串
* @return 字符串
*/
public static String toUnicode(String value)
{
if (value == null)
{
return "";
}
byte[] tmp = new byte[value.length()];
int I = 0;
while (I < value.length())
{
tmp[I] = (byte) (value.charAt(I) & 0x00FF);// 双字节
I++;
}
String result = new String(tmp);
return result;
}
/**
* 把字符串的编码从GB2312转换为ISO8859_1.
*
* @param str
* 输入字符串.
* @return 编码后的字符串.
*/
public static String GB_2_ISO8859(String str)
{
if (str == null)
{
return "";
}
try
{
str = new String(str.getBytes("GBK"), "iso-8859-1");
return str;
} catch (Exception ex)
{
}
return null;
}
/**
* 把字符串的编码从ISO8859_1转换为GB2312.
*
* @param str
* 输入字符串.
* @return 编码后的字符串.
*/
public static String ISO8859_2_Gb(String str)
{
if (str == null)
{
return "";
}
try
{
str = new String(str.getBytes("iso-8859-1"), "GBK");
return str;
} catch (Exception ex)
{
}
return null;
}
/*
* 把ASCII码转换为中文字符串(String)
*/
public static String ASCII2String(int[] ASCIIs) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < ASCIIs.length; i++) {
sb.append((char) ascii2Char(ASCIIs[i]));
}
return sb.toString();
}
public static String ASCII2String(String ASCIIs) {
String[] ASCIIss = ASCIIs.split(",");
StringBuffer sb = new StringBuffer();
for (int i = 0; i < ASCIIss.length; i++) {
sb.append((char) ascii2Char(Integer.parseInt(ASCIIss[i])));
}
return sb.toString();
}
/*
* 把中文字符串(String)转换为ASCII码
*/
public static int[] string2ASCII(String str)
{
if (str == null || "".equals(str)) {
return null;
}
char[] chars = str.toCharArray();
int[] asciiArray = new int[chars.length];
for (int i = 0; i < chars.length; i++) {
asciiArray[i] = char2ASCII(chars[i]);
}
return asciiArray;
}
/**
* 数字字符串转ASCII码字符串
*
* @param String字符串
* @return ASCII字符串
*/
public static String StringToAsciiString(String content) {
String result = "";
int max = content.length();
for (int i = 0; i < max; i++) {
char c = content.charAt(i);
String b = Integer.toHexString(c);
result = result + b;
}
return result;
}
/**
* ASCII码字符串转数字字符串
*
* @param String ASCII字符串
* @return 字符串
*/
public static String AsciiStringToString(String content) {
String result = "";
int length = content.length() / 2;
for (int i = 0; i < length; i++) {
String c = content.substring(i * 2, i * 2 + 2);
int a = hexStringToAlgorism(c);
char b = (char) a;
String d = String.valueOf(b);
result += d;
}
return result;
}
/**
* 十六进制字符串装十进制
*
* @param hex 十六进制字符串
* @return 十进制数值
*/
public static int hexStringToAlgorism(String hex) {
hex = hex.toUpperCase();
int max = hex.length();
int result = 0;
for (int i = max; i > 0; i--) {
char c = hex.charAt(i - 1);
int algorism = 0;
if (c >= '0' && c <= '9') {
algorism = c - '0';
} else {
algorism = c - 55;
}
result += Math.pow(16, max - i) * algorism;
}
return result;
}
/*
* 字符(char)转换为ASCII码
*/
public static int char2ASCII(char c) {
return (int) c;
}
/*
* ASCII码转换为字符(char)
*/
public static char ascii2Char(int ASCII) {
return (char) ASCII;
}
public static String hexToString(String hexString) throws NumberFormatException, UnsupportedEncodingException {
if(hexString.length() % 2 != 0) {
throw new IllegalArgumentException("error");
}
char[] chars = hexString.toCharArray();
byte[] b = new byte[chars.length / 2];
for(int i = 0, j = b.length; i < j; i++) {
int high = Character.digit(chars[2 * i], 16) << 4;
int low = Character.digit(chars[2 * i + 1], 16);
b[i] = (byte)(high | low);
}
return new String(b);
}
public static String stringToHex(String s) throws UnsupportedEncodingException {
byte[] b = s.getBytes();
char[] hex = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
char[] newChar = new char[b.length * 2];
for(int i = 0, j = b.length; i < j; i++) {
newChar[2 * i] = hex[(b[i] & 0xf0) >> 4];
newChar[2 * i + 1] = hex[b[i] & 0xf];
}
return new String(newChar);
}
/**
* 将short类型转换为byte数组
* @Title: shortToBytes
* @Description: TODO
* @param @param sNum
* @param @return
* @return byte[]
* @throws
*/
public static byte[] shortToBytes(short sNum) {
byte[] bytesRet = new byte[2];
bytesRet[0] = (byte) ((sNum >> 8) & 0xFF);
bytesRet[1] = (byte) (sNum & 0xFF);
return bytesRet;
}
/**
* 将byte数组转换为short类型
* @Title: bytesToShort
* @Description: TODO
* @param @param bytesShort
* @param @return
* @return short
* @throws
*/
public static short bytesToShort(byte[] bytesShort) {
short sRet = 0;
sRet += (bytesShort[0] & 0xFF) << 8;
sRet += bytesShort[1] & 0xFF;
return sRet;
}
/**
* @功能: 10进制串转为BCD码
* @参数: 10进制串 String
* @结果: BCD码 byte[]
*
*
*/
public static byte[] decimal2Bcd(String decimal) {
int len = decimal.length();
int mod = len % 2;
if (mod != 0) {
decimal = "0" + decimal;
len = decimal.length();
}
byte abt[] = new byte[len];
if (len >= 2) {
len = len / 2;
}
byte bbt[] = new byte[len];
abt = decimal.getBytes();
int j, k;
for (int p = 0; p < decimal.length() / 2; p++) {
if ((abt[2 * p] >= '0') && (abt[2 * p] <= '9')) {
j = abt[2 * p] - '0';
} else if ((abt[2 * p] >= 'a') && (abt[2 * p] <= 'z')) {
j = abt[2 * p] - 'a' + 0x0a;
} else {
j = abt[2 * p] - 'A' + 0x0a;
}
if ((abt[2 * p + 1] >= '0') && (abt[2 * p + 1] <= '9')) {
k = abt[2 * p + 1] - '0';
} else if ((abt[2 * p + 1] >= 'a') && (abt[2 * p + 1] <= 'z')) {
k = abt[2 * p + 1] - 'a' + 0x0a;
} else {
k = abt[2 * p + 1] - 'A' + 0x0a;
}
int a = (j << 4) + k;
byte b = (byte) a;
bbt[p] = b;
}
return bbt;
}
/**
* @功能: BCD码转为10进制串(阿拉伯数据)
* @参数: BCD码
* @结果: 10进制串
*
* 例如:“11”---> 3131
*/
public static String bcd2Str(byte[] bytes) {
StringBuffer temp = new StringBuffer(bytes.length * 2);
for (int i = 0; i < bytes.length; i++) {
temp.append((byte) ((bytes[i] & 0xf0) >>> 4));
temp.append((byte) (bytes[i] & 0x0f));
}
return temp.toString().substring(0, 1).equalsIgnoreCase("0") ? temp
.toString().substring(1) : temp.toString();
}
private static byte asc_to_bcd(byte asc) {
byte bcd;
if ((asc >= '0') && (asc <= '9'))
bcd = (byte) (asc - '0');
else if ((asc >= 'A') && (asc <= 'F'))
bcd = (byte) (asc - 'A' + 10);
else if ((asc >= 'a') && (asc <= 'f'))
bcd = (byte) (asc - 'a' + 10);
else
bcd = (byte) (asc - 48);
return bcd;
}
/**
* ASCII--->BCD
* @param ascii
* @param asc_len
* @return
*/
private static byte[] ASCII_To_BCD(byte[] ascii, int asc_len) {
byte[] bcd = new byte[asc_len / 2];
int j = 0;
for (int i = 0; i < (asc_len + 1) / 2; i++) {
bcd[i] = asc_to_bcd(ascii[j++]);
bcd[i] = (byte) (((j >= asc_len) ? 0x00 : asc_to_bcd(ascii[j++])) + (bcd[i] << 4));
System.out.format("%02X\n", bcd[i]);
}
return bcd;
}
/**
* BCD码转为字符串
* @param bytes
* @return
*/
public static String bcd2String(byte[] bytes) {
char temp[] = new char[bytes.length * 2], val;
for (int i = 0; i < bytes.length; i++) {
val = (char) (((bytes[i] & 0xf0) >> 4) & 0x0f);
temp[i * 2] = (char) (val > 9 ? val + 'A' - 10 : val + '0');
val = (char) (bytes[i] & 0x0f);
temp[i * 2 + 1] = (char) (val > 9 ? val + 'A' - 10 : val + '0');
}
return new String(temp);
}
/**
* MD5摘要大写
*/
public static String getMD5(String str) {
// 生成一个MD5加密计算摘要
MessageDigest md;
try {
md = MessageDigest.getInstance("MD5");
// 计算md5函数
md.update(str.getBytes());
// digest()最后确定返回md5 hash值,返回值为8为字符串。因为md5 hash值是16位的hex值,实际上就是8位的字符
// BigInteger函数则将8位的字符串转换成16位hex值,用字符串来表示;得到字符串形式的hash值,并且转成大写
return new BigInteger(1, md.digest()).toString(16).toUpperCase();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return null;
}
}
}
字符编码处理工具类
最新推荐文章于 2021-02-13 08:26:58 发布