package com.util;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.regex.Pattern;
//帮助类
public class ConvertUtil {
public final static char[] BToA = "0123456789abcdef".toCharArray();
private ConvertUtil() {
}
/**
* UTF-8编码 转换为对应的 汉字
*
* URLEncoder.encode("上海", "UTF-8") ---> %E4%B8%8A%E6%B5%B7
* URLDecoder.decode("%E4%B8%8A%E6%B5%B7", "UTF-8") --> 上 海
*
* convertUTF8ToString("E4B88AE6B5B7") E4B88AE6B5B7 --> 上海
*
* @param s
* @return
*/
public static String convertUTF8ToString(String s) {
if (s == null || s.equals("")) {
return null;
}
try {
s = s.toUpperCase();
int total = s.length() / 2;
int pos = 0;
byte[] buffer = new byte[total];
for (int i = 0; i < total; i++) {
int start = i * 2;
buffer[i] = (byte) Integer.parseInt(
s.substring(start, start + 2), 16);
pos++;
}
return new String(buffer, 0, pos, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return s;
}
/**
* 将文件名中的汉字转为UTF8编码的串,以便下载时能正确显示另存的文件名.
*
* @param s
* 原串
* @return
*/
public static String convertStringToUTF8(String s) {
if (s == null || s.equals("")) {
return null;
}
StringBuffer sb = new StringBuffer();
try {
char c;
for (int i = 0; i < s.length(); i++) {
c = s.charAt(i);
if (c >= 0 && c <= 255) {
sb.append(c);
} else {
byte[] b;
b = Character.toString(c).getBytes("utf-8");
for (int j = 0; j < b.length; j++) {
int k = b[j];
if (k < 0)
k += 256;
sb.append(Integer.toHexString(k).toUpperCase());
// sb.append("%" +Integer.toHexString(k).toUpperCase());
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return sb.toString();
}
public static String getNowDate() throws Exception {
try {
java.util.Calendar c = java.util.Calendar.getInstance();
java.text.SimpleDateFormat f = new java.text.SimpleDateFormat(
"MM月dd日 HH:mm");
return f.format(c.getTime());
} catch (Exception e) {
throw e;
}
}
/**
* int值转换为4位的十六进制
*
* @param s
* @return
* @throws Exception
*/
public static String format4Hex(int n) throws Exception {
try {
String str = Integer.toHexString(n);
int len = str.length();
String hexStr = "";
for (int i = len; i < 4; i++) {
if (i == len)
hexStr = "0";
else
hexStr = hexStr + "0";
}
return hexStr + str.toUpperCase();
} catch (Exception e) {
throw e;
}
}
/**
* 二进制转换成十六进制字符串
*
* @param binaryStr
* @return
* @throws Exception
*/
public static String BINTOHEX(String binstr) throws Exception {
try {
int len = binstr.length() / 4;
String res = "";
for (int i = 0; i < len; i++) {
char[] ch = new char[4];
binstr.getChars(i * 4, i * 4 + 4, ch, 0);
res = res + switchS(trans1(ch));
}
return res;
} catch (Exception e) {
throw e;
}
}
public static int trans1(char[] ch) throws Exception {
try {
int sum = 0;
for (int i = 0; i < 4; i++) {
int y = 8;
if (ch[i] == '1') {
for (int j = 1; j <= i; j++) {
y = y / 2;
}
sum = sum + y;
}
}
return sum;
} catch (Exception e) {
throw e;
}
}
public static String switchS(int i) throws Exception {
try {
String s = "";
switch (i) {
case 10:
s = "A";
break;
case 11:
s = "B";
break;
case 12:
s = "C";
break;
case 13:
s = "D";
break;
case 14:
s = "E";
break;
case 15:
s = "F";
default:
s = "" + i;
}
return s;
} catch (Exception e) {
throw e;
}
}
/**
* 十六进制转换成二进制
*
* @param str
* @return
* @throws Exception
*/
public static String HEXTOBIN(String str) throws Exception {
try {
String resultStr = "";
String str2 = "";
for (int i = 0; i < str.length(); i++) {
int caseInt = 0;
String oneStr = str.substring(i, i + 1).toUpperCase();
String numstr = "0123456789ABCDEF";
caseInt = numstr.indexOf(oneStr);
switch (caseInt) {
case 0:
str2 = "0000";
break;
case 1:
str2 = "0001";
break;
case 2:
str2 = "0010";
break;
case 3:
str2 = "0011";
break;
case 4:
str2 = "0100";
break;
case 5:
str2 = "0101";
break;
case 6:
str2 = "0110";
break;
case 7:
str2 = "0111";
break;
case 8:
str2 = "1000";
break;
case 9:
str2 = "1001";
break;
case 10:
str2 = "1010";
break;
case 11:
str2 = "1011";
break;
case 12:
str2 = "1100";
break;
case 13:
str2 = "1101";
break;
case 14:
str2 = "1110";
break;
case 15:
str2 = "1111";
break;
}
resultStr += str2;
}
return resultStr;
} catch (Exception e) {
throw e;
}
}
/**
* ASCII码转换为十六进制
*
* @param ch
* @return
* @throws Exception
*/
public static String ASCTOHEX(String ch) throws Exception {
try {
char[] chararr = ch.toCharArray();
String str = "";
for (int i = 0; i < chararr.length; i++) {
int a = (int) chararr[i]; // ASCII
str += Integer.toString(a, 16); // 十六进制
}
return str.toUpperCase();
} catch (Exception e) {
throw e;
}
}
/**
* 十六进制字符串转换为ASCII
*
* @param hexStr
* @return
* @throws Exception
*/
public static String HEXTOASC(String hex) throws Exception {
try {
StringBuilder ascStr = new StringBuilder();
StringBuilder temp = new StringBuilder();
// 每2个为一组转换为ASCII字符
for (int i = 0; i < hex.length() - 1; i += 2) {
String output = hex.substring(i, (i + 2));
int decimal = Integer.parseInt(output, 16);
ascStr.append((char) decimal);
temp.append(decimal);
}
return ascStr.toString();
} catch (Exception e) {
throw e;
}
}
/**
* 汉字转换为十六进制
*
* @param content
* @return
* @throws Exception
*/
public static String GBTOHEX(String strHan) throws Exception {// 将汉字转换为16进制数
try {
try {
String OKStr = "";
for (int i = 0; i < strHan.length(); i++) {
OKStr += ZFTo16(strHan.substring(i, i + 1)) + "20";
}
return OKStr;
} catch (Exception e) {
throw e;
}
} catch (Exception e) {
throw e;
}
}
// 字符转换成十六进制数字
private static String ZFTo16(String str)
throws UnsupportedEncodingException {
String Str = "";
byte[] bi = str.getBytes();
if (str == "") {
return "";
}
for (int i = 0; i < bi.length; i++) {
Str += toHex(bi[i]);
}
if (Str.length() == 2) {
Str = "00" + Str;
}
return Str;
}
public static final String toHex(byte b) {
return ("" + "0123456789ABCDEF".charAt(0xf & b >> 4) + "0123456789ABCDEF"
.charAt(b & 0xf));
}
/**
* 16进制数转换为汉字
*
* @param strHex
* @return
* @throws Exception
*/
public static String HEXTOGB(String strHex) throws Exception {
try {
String AA = "";
strHex = strHex.replace("00", "");
String[] s = strHex.split("20");
for (int i = 0; i < s.length; i++) {
AA += ZF16ToZF(s[i]);
}
return AA;
} catch (Exception e) {
throw e;
}
}
public static String ZF16ToZF(String str16) throws Exception {
try {
String ZFStr = "";
if (str16 == "") {
return "";
}
if (str16.length() % 2 != 0) {
str16 += "20";
}
byte[] bi = new byte[str16.length() / 2];
for (int i = 0; i < bi.length; i++) {
bi[i] = hexStringToBytes(str16.substring(i * 2, i * 2 + 2));
}
try {
ZFStr = new String(bi, "GBK");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return ZFStr;
} catch (Exception e) {
throw e;
}
}
public static byte hexStringToBytes(String hexString) throws Exception {
try {
hexString = hexString.toUpperCase();
char[] hexChars = hexString.toCharArray();
byte d;
d = (byte) (charToByte(hexChars[0]) << 4 | charToByte(hexChars[1]));
return d;
} catch (Exception e) {
throw e;
}
}
public static byte charToByte(char c) {
return (byte) "0123456789ABCDEF".indexOf(c);
}
/**
* 字符串转换为4位字符串 14----0014
*
* @param hexString
* @return
* @throws Exception
*/
public static String getHexString(String hexString) throws Exception {
try {
String hexStr = "";
for (int i = hexString.length(); i < 4; i++) {
if (i == hexString.length())
hexStr = "0";
else
hexStr = hexStr + "0";
}
return hexStr + hexString;
} catch (Exception e) {
throw e;
}
}
/**
* 把16进制字符串转换成字节数组 byte[]
*
* @param hex
* @return
* @throws Exception
*/
public static byte[] hexStringToByte(String hex) throws Exception {
try {
int len = (hex.length() / 2);
byte[] result = new byte[len];
char[] achar = hex.toCharArray();
for (int i = 0; i < len; i++) {
int pos = i * 2;
result[i] = (byte) (toByte(achar[pos]) << 4 | toByte(achar[pos + 1]));
}
return result;
} catch (Exception e) {
throw e;
}
}
private static byte toByte(char c) {
byte b = (byte) "0123456789ABCDEF".indexOf(c);
return b;
}
/**
* 把字节数组byte[]转换成16进制字符串
*
* @param bArray
* @return
* @throws Exception
*/
public static final String bytesToHexString(byte[] bArray) throws Exception {
try {
if (bArray == null) {
return "";
}
StringBuffer sb = new StringBuffer(bArray.length);
String sTemp;
for (int i = 0; i < bArray.length; i++) {
sTemp = Integer.toHexString(0xFF & bArray[i]);
if (sTemp.length() < 2)
sb.append(0);
sb.append(sTemp.toUpperCase());
}
return sb.toString();
} catch (Exception e) {
throw e;
}
}
/**
* 从一个byte[]数组中截取一部分
*
* @param src
* @param begin
* @param count
* @return
*/
public static byte[] subBytes(byte[] src, int begin, int count) {
byte[] bs = new byte[count];
for (int i = begin; i < begin + count; i++)
bs[i - begin] = src[i];
return bs;
}
/**
* 把字节数组转换为对象
*
* @param bytes
* @return
* @throws IOException
* @throws ClassNotFoundException
*/
public static final Object bytesToObject(byte[] bytes) throws IOException,
ClassNotFoundException {
ByteArrayInputStream in = new ByteArrayInputStream(bytes);
ObjectInputStream oi = new ObjectInputStream(in);
Object o = oi.readObject();
oi.close();
return o;
}
/**
* 把可序列化对象转换成字节数组
*
* @param s
* @return
* @throws IOException
*/
public static final byte[] objectToBytes(Serializable s) throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
ObjectOutputStream ot = new ObjectOutputStream(out);
ot.writeObject(s);
ot.flush();
ot.close();
return out.toByteArray();
}
public static final String objectToHexString(Serializable s)
throws Exception {
return bytesToHexString(objectToBytes(s));
}
public static final Object hexStringToObject(String hex) throws Exception {
return bytesToObject(hexStringToByte(hex));
}
/**
* @函数功能: BCD码转为10进制串(阿拉伯数据)
* @输入参数: BCD码
* @输出结果: 10进制串
*/
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();
}
/**
* @函数功能: 10进制串转为BCD码
* @输入参数: 10进制串
* @输出结果: BCD码
*/
public static byte[] str2Bcd(String asc) {
int len = asc.length();
int mod = len % 2;
if (mod != 0) {
asc = "0" + asc;
len = asc.length();
}
byte abt[] = new byte[len];
if (len >= 2) {
len = len / 2;
}
byte bbt[] = new byte[len];
abt = asc.getBytes();
int j, k;
for (int p = 0; p < asc.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;
}
public static String BCD2ASC(byte[] bytes) throws Exception {
try {
StringBuffer temp = new StringBuffer(bytes.length * 2);
for (int i = 0; i < bytes.length; i++) {
int h = ((bytes[i] & 0xf0) >>> 4);
int l = (bytes[i] & 0x0f);
temp.append(BToA[h]).append(BToA[l]);
}
return temp.toString();
} catch (Exception e) {
throw e;
}
}
/**
* 两字符数组异或
*
* @throws Exception
*/
public static byte[] byteArrXor(byte[] arr1, byte[] arr2, int len)
throws Exception {
try {
byte[] dest = new byte[len];
if ((arr1.length < len) || (arr2.length < len)) {
return null;
}
for (int i = 0; i < len; i++) {
dest[i] = (byte) (arr1[i] ^ arr2[i]);
}
return dest;
} catch (Exception e) {
throw e;
}
}
/**
* 获取明天的日期
*
* @return
*/
@SuppressWarnings("static-access")
public static String getTomorrowDate() {
Date date = new Date();// 取时间
Calendar calendar = new GregorianCalendar();
calendar.setTime(date);
calendar.add(calendar.DATE, 1);// 把日期往后增加一天.整数往后推,负数往前移动
date = calendar.getTime(); // 这个时间就是日期往后推一天的结果
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
String dateString = formatter.format(date);
return dateString;
}
/**
* 获得今天的时间
*
* @return
*/
public static String getTodayTime() {
Date date = new Date();// 取时间
Calendar calendar = new GregorianCalendar();
calendar.setTime(date);
date = calendar.getTime(); // 这个时间就是日期往后推一天的结果
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateString = formatter.format(date);
return dateString;
}
/**
* @Method: padLeftStr
* @Description: 字符串补足4、12(n)位 【左补0】
* @param @param str
* @param @param n
* @param @return
* @return String
* @throws
*/
public static String padLeftStr(String str, int n) { //
if (str.contains(".0") || str.contains(".00")) {
str = str.replace(".00", "").replace(".0", "");
}
String returnStr = "";
int len = str.length();
if (len < n) {
for (int i = 0; i < n - len; i++) {
returnStr += "0";
}
}
returnStr += str;
return returnStr;
}
/**
* @Method: padLeftStr
* @Description: 字符串补足12(n)位 【左补空格】
* @param @param str
* @param @param n
* @param @return
* @return String
* @throws
*/
public static String padLeftKong(String str, int n) {
String returnStr = "";
int len = str.length();
if (len < n) {
for (int i = 0; i < n - len; i++) {
returnStr += " ";
}
}
returnStr += str;
return returnStr;
}
/**
* 获得今天的时间,精确到毫秒
*
* @return
*/
public static String getTodayTime2() {
Date date = new Date();// 取时间
Calendar calendar = new GregorianCalendar();
calendar.setTime(date);
date = calendar.getTime(); // 这个时间就是日期往后推一天的结果
SimpleDateFormat formatter = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss:SSS");
String dateString = formatter.format(date);
return dateString;
}
// 判断字符串是数字字符串,是则转换为int,否返回-1
public static boolean isNumberic(String message) {
Pattern pattern = Pattern.compile("[0-9]*");
return pattern.matcher(message).matches();
}
public static void main(String[] args) throws Exception {
// String s = "4130";
// System.out.println(HEXTOASC(s));
// System.out.println(getTodayTime());
// System.out.println(isNumberic("0024") == true);
// String hexStr = "003620007C20005020004F20005320003420003120003020003920003920007C20CDA820D3C320BDD320BFDA20007C20003020007C20003020007C20003120007C20003020007C20";
// System.out.println(HEXTOGB(hexStr));
// String str = "6|POS41099|通用接口|0|0|1|0|";
// System.out.println(GBTOHEX(str));// BDF620D4F620BCD320BBFD20B7D620007C20
// System.out.println(toHex((byte)-67));
String str = "姓名";
System.out.println(str.getBytes("utf-8"));
}
}
【备份】常用的二进制、十六进制、字符串以及汉字之间的转换。【Java】
最新推荐文章于 2023-02-27 18:08:10 发布