Hexadecimal (tools) between (Hex) and byte array

package com.coinflex.common;

/**
 * Conversion between hexadecimal (Hex) and byte array
 * 
 * @date 2019-09-05
 */
public class Hex2ByteUtils {
    /**
     * Byte hexadecimal turn
     * 
     * @Param B byte byte conversion is required
     * @Return Hex converted string
      * / 
    public  static String byteToHex ( byte B) {
        String hex = Integer.toHexString(b & 0xFF);
        if (hex.length() < 2) {
            hex = "0" + hex;
        }
        return hex;
    }

    /**
     Hexadecimal byte array * rpm
     * 
     * @Param bytes need to convert byte array
     * @Return Hex converted 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();
    }

    /**
     * Hex string byte transfer
     * 
     * @Param inHex Hex string to be converted
     * @Return byte converted
      * / 
    public  static  byte hexToByte (String inHex) {
         return ( byte ) the Integer.parseInt (inHex, 16 );
    }

    /**
     * Hex string transfer byte array
     * 
     * @Param inHex Hex string to be converted
     * @Return byte array converted result
      * / 
    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;
    }
}

 

Guess you like

Origin www.cnblogs.com/xy-ouyang/p/11612797.html