物联网中常用的方法,16进制二进制之间的转换

由于最近公司做的是关于物联网的项目,所以经常用到进制转换的功能,所以自己写了一些并且收集了一些方法,供大家使用

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/***
 * 字节和16进制转换类
 * 
 * @author Administrator
 *
 */
public class ByteUtils {
    /**
     * byte[] 转为16进制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;
    }

    /**
     * 从一个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;
    }

    // 转化十六进制编码为字符串
    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+"";
    }
    /**
     * 十六进制转成二进制
     * @param hex
     * @return
     */
    public static int binary(String hex){
        return Integer.parseInt(hex, 16);
    }
    /**
     * 二进制转成十六进制
     * @param hex
     * @return
     */
    public static String hex(int var){
        if(var<0){
            return "错误";
        }
        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;
    }

    /** 
     * 字节数组转16进制 
     * @param bytes 需要转换的byte数组 
     * @return  转换后的Hex字符串 
     */  
    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();  
    }
    /** 
     * 字节转十六进制 
     * @param b 需要进行转换的byte字节 
     * @return  转换后的Hex字符串 
     */  
    public static String byteToHex(byte b){  
        String hex = Integer.toHexString(b & 0xFF);  
        if(hex.length() < 2){  
            hex = "0" + hex;  
        }  
        return hex;  
    }

    /** 
     * Hex字符串转byte 
     * @param inHex 待转换的Hex字符串 
     * @return  转换后的byte 
     * 需注意的是,Hex的字符串必须为十六进制的字符,否则会抛出异常。Hex的范围为0x00到0xFF
     */  
    public static byte hexToByte(String inHex){  
       return (byte)Integer.parseInt(inHex,16);  
    }  
    /** 
     * hex字符串转byte数组 
     * @param inHex 待转换的Hex字符串 
     * @return  转换后的byte数组结果 
     */  
    public static byte[] hexToByteArray(String inHex){  
        int hexlen = inHex.length();  
        byte[] result;  
        if (hexlen % 2 == 1){  
            //奇数  
            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;   
    } 
}
 

猜你喜欢

转载自blog.csdn.net/u010460625/article/details/108482238