• 【Java】将字节转换成十六进制、BCD码输出


      1 public class HexUtils {  
      2   
      3     public static void main(String[] args) {
      4         byte []out = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
      5                 (byte)0x10, (byte)0x11, (byte)0x12, (byte)127, (byte)128, (byte)255 };
      6                 
      7 
      8         System.out.println( bytesToHexString((byte)10));          
      9         System.out.println( bytesToHexString(out) );
     10         System.out.println( bytesToHexString(out, 0) );
     11         System.out.println( bytesToHexString(out, 1) );
     12         
     13         byte []b1 = hexStringToBytes("0123456789ABCDEF");
     14         for( int i = 0; i < b1.length; i++ ) {
     15             System.out.println("[" + i + "]:" + b1[i]);
     16         }
     17         System.out.println( bytesToHexString(b1) );
     18         
     19         byte []b2 = bcdStringToBytes("1234567890123456");
     20         for( int i = 0; i < b2.length; i++ ) {
     21             System.out.println("[" + i + "]:" + b2[i]);
     22         }
     23         System.out.println( bytesToHexString(b2) );
     24         
     25         byte []bcdBytes = {0, 1, 2, 3, 4, 5, 6, 7, 9, 111, 
     26                 10, 11, 21, 31, 41, 51, 61, 71, 81, 91, 99};
     27         System.out.println( bcdToHexString(bcdBytes) );
     28     }  
     29   
     30     public static String bytesToHexString(byte src) {  
     31         int v = src & 0xFF;  
     32         String hv = Integer.toHexString(v);  
     33         return hv;  
     34     }  
     35   
     36     public static String bytesToHexString(byte[] src) {  
     37         StringBuilder stringBuilder = new StringBuilder("");  
     38         if (src == null || src.length <= 0) {  
     39             return null;  
     40         }  
     41         for (int i = 0; i < src.length; i++) {  
     42             int v = src[i] & 0xFF;  
     43             String hv = Integer.toHexString(v);  
     44             hv = hv.toUpperCase();  
     45             if (hv.length() < 2) {  
     46                 stringBuilder.append(0);  
     47             }  
     48             stringBuilder.append(hv);
     49         }  
     50         return stringBuilder.toString();  
     51     }  
     52   
     53     public static String bytesToHexString(byte[] src, int mode) {  
     54         StringBuilder stringBuilder = new StringBuilder("");  
     55         if (src == null || src.length <= 0) {  
     56             return null;  
     57         }  
     58         for (int i = 0; i < src.length; i++) {  
     59         
     60             if( i != 0 && i % 16 == 0 ) {
     61                 stringBuilder.append("
    ");
     62             }
     63         
     64             if( mode == 1 ) {
     65                 stringBuilder.append("0x"); 
     66             }
     67             
     68             int v = src[i] & 0xFF;  
     69             String hv = Integer.toHexString(v);  
     70             if (hv.length() < 2) {  
     71                 stringBuilder.append(0);  
     72             }  
     73             stringBuilder.append(hv);
     74             
     75             if( mode == 1 && i+1 != src.length ) {
     76                 stringBuilder.append(',');
     77             }
     78             stringBuilder.append(" ");
     79         }  
     80         return stringBuilder.toString();  
     81     }
     82   
     83     public static String bcdToHexString(byte[] src) {
     84         StringBuilder stringBuilder = new StringBuilder("");  
     85         if (src == null || src.length <= 0) {  
     86             return null;  
     87         }  
     88         for (int i = 0; i < src.length; i++) {
     89             int v = src[i] & 0xFF;
     90             int v1 = v / 10;
     91             int v2 = v % 10;
     92             
     93             if( v >= 0 && v < 100 ) { 
     94                 stringBuilder.append( v1 );
     95                 stringBuilder.append( v2 );
     96             }
     97         }  
     98         return stringBuilder.toString();      
     99     }  
    100 
    101     public static byte[] hexStringToBytes(String hexString) {  
    102         if (hexString == null || hexString.equals("") || hexString.length() % 2 != 0) {  
    103             return null;  
    104         }  
    105         hexString = hexString.toUpperCase();  
    106         int length = hexString.length() / 2;  
    107         char[] hexChars = hexString.toCharArray();  
    108         byte[] d = new byte[length];  
    109         for (int i = 0; i < length; i++) {  
    110             int pos = i * 2;  
    111             d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));  
    112         }  
    113         return d;  
    114     }  
    115     
    116     public static byte[] bcdStringToBytes(String hexString) {  
    117         if (hexString == null || hexString.equals("") || hexString.length() % 2 != 0) {  
    118             return null;  
    119         }  
    120         hexString = hexString.toUpperCase();  
    121         int length = hexString.length() / 2;  
    122         char[] hexChars = hexString.toCharArray();  
    123         byte[] d = new byte[length];  
    124         for (int i = 0; i < length; i++) {  
    125             int pos = i * 2;  
    126             d[i] = (byte) (charToByte(hexChars[pos]) * 10 + charToByte(hexChars[pos + 1]));  
    127         }  
    128         return d;  
    129     } 
    130     
    131     private static byte charToByte(char c) {  
    132         return (byte) "0123456789ABCDEF".indexOf(c);  
    133     }  
    134 }  
  • 相关阅读:
    RabbitMQ详解(二)——
    Redis6详解(二)——常用命令
    MybatisPlus(二)——
    数据结构与算法(五)——树
    数据结构与算法(四)——队列
    数据结构与算法(三)——栈
    MybatisPlus(一)——
    Docker详解(一)——
    kafka详解(一)——
    FIle类操作
  • 原文地址:https://www.cnblogs.com/utank/p/7275450.html
Copyright © 2020-2023  润新知