Since the company is doing projects about the Internet of Things recently, it often uses the function of hexadecimal conversion, so I wrote some and collected some methods for everyone to use
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/***
* Byte and hexadecimal conversion class
*
* @author Administrator
*
*/
public class ByteUtils { /** * byte[] to hexadecimal String */ 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; }
/**
* Take a part from a byte[] array
*
* @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; }
// Convert the hexadecimal code to a string
public static String toStringHex(String s) { byte[] baKeyword = new byte[s.length() / 2]; for (int i = 0; i <baKeyword.length; i++) { try { baKeyword[i] = (byte) (0xff & Integer.parseInt(s.substring(i * 2, i * 2 + 2), 16)); } catch (Exception e) { e.printStackTrace( ); } }
try {
s = new String(baKeyword, "utf-8");// UTF-16le:Not
} catch (Exception e1) {
e1.printStackTrace();
}
return s;
}
/****
* 获取年份的后两位
* @return
*/
public static String getYear() {
Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
year = year - 2000;
return year+"";
}
public static String month(){
Calendar calendar = Calendar.getInstance();
int month = calendar.get(Calendar.MONTH)+1;
return month<10?"0"+month:month+"";
}
public static String day(){
Calendar calendar = Calendar.getInstance();
int date = calendar.get(Calendar.DATE);
return date<10?"0"+date:date+"";
}
public static String hour(){
Calendar calendar = Calendar.getInstance();
int hour = calendar.get(Calendar.HOUR_OF_DAY);
return hour<10?"0"+hour:hour+"";
}
public static String minute(){
Calendar calendar = Calendar.getInstance();
int minute = calendar.get(Calendar.MINUTE);
return minute<10?"0"+minute:minute+"";
}
public static String second(){ Calendar calendar = Calendar.getInstance(); int second = calendar.get(Calendar.SECOND); return second<10?"0"+second:second+""; } /** * Sixteen Convert from hexadecimal to binary * @param hex * @return */ public static int binary(String hex){ return Integer.parseInt(hex, 16); } /** * Convert from binary to hexadecimal * @param hex * @return */ public static String hex(int var){ if(var<0){ return "error"; }
String hex = Integer.toHexString(var);
if(var<=15){
hex ="0"+hex;
}
return hex;
}
public static String getLastMonthTime(){
Calendar calendar = Calendar.getInstance();
//calendar.add(Calendar.MONTH, -1);
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH)+1;
int date = calendar.get(Calendar.DATE);
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int minute = calendar.get(Calendar.MINUTE);
int second = calendar.get(Calendar.SECOND);
return year+"-"+(month<10?"0"+month:month)+"-"+(date<10?"0"+date:date)+" "+(hour<10?"0"+hour:hour)+":"+(minute<10?"0"+minute:minute)+":"+(second<10?"0"+second:second);
}
public static void main(String[] args) {
String year = getLastMonthTime();
System.out.println(year);
}
/***
* 16进制想加
* @param hexdata
* @return
*/
public static String makeChecksum(String hexdata) {
if (hexdata == null || hexdata.equals("")) {
return "00";
}
hexdata = hexdata.replaceAll(" ", "");
int total = 0;
int len = hexdata.length();
if (len % 2 != 0) {
return "00";
}
int num = 0;
while (num < len) {
String s = hexdata.substring(num, num + 2);
total += Integer.parseInt(s, 16);
num = num + 2;
}
return hexInt(total);
}
private static String hexInt(int total) {
int a = total / 256;
int b = total % 256;
if (a > 255) {
return hexInt(a) + format(b);
}
return format(a) + format(b);
}
private static String format(int hex) {
String hexa = Integer.toHexString(hex);
int len = hexa.length();
if (len < 2) {
hexa = "0" + hexa;
}
return hexa;
}
/**
* Byte array to hexadecimal
* @param bytes The byte array to be converted
* @return The converted Hex string
*/
public static String bytesToHex(byte[] bytes) {
StringBuffer sb = new StringBuffer();
for(int i = 0; i <bytes.length; i++) {
String hex = Integer.toHexString(bytes[i] & 0xFF);
if(hex.length() <2){
sb.append(0);
}
sb.append(hex);
}
return sb.toString();
}
/**
* Byte to hexadecimal
* @param b Byte to be converted
* @return Hex string after conversion
*/
public static String byteToHex(byte b){
String hex = Integer.toHexString(b & 0xFF);
if(hex.length() < 2){
hex = "0" + hex;
}
return hex;
}
/**
* Hex string to byte
* @param inHex Hex string to be converted
* @return converted byte
* It should be noted that the Hex string must be a hexadecimal character, otherwise an exception will be thrown . The range of Hex is 0x00 to 0xFF
*/
public static byte hexToByte(String inHex){
return (byte)Integer.parseInt(inHex,16);
}
/**
* hex string to byte array
* @param inHex Hex to be converted String
* @return Result of converted byte array
*/
public static byte[] hexToByteArray(String inHex){
int hexlen = inHex.length();
byte[] result;
if (hexlen% 2 == 1){
// Odd
hexlen++;
result = new byte[(hexlen/2)];
inHex="0"+inHex;
}else {
//偶数
result = new byte[(hexlen/2)];
}
int j=0;
for (int i = 0; i < hexlen; i+=2){
result[j]=hexToByte(inHex.substring(i,i+2));
j++;
}
return result;
}
}