分享一个 常用的 hex 转 characters或integer

发布时间:2020-08-06 02:04:29 作者:yidinshi
来源:网络 阅读:846

 这个如果不熟的话还是要查找许多资料和时间来解决这个问题的,下面分享这样一个类。

这样方便大家节省许多功夫去查找资料。

代码:

 

  1. package org.apache.commons.codec.binary; 
  2.  
  3. import org.apache.commons.codec.BinaryDecoder; 
  4. import org.apache.commons.codec.BinaryEncoder; 
  5. import org.apache.commons.codec.DecoderException; 
  6. import org.apache.commons.codec.EncoderException; 
  7.  
  8. /** 
  9.  * Hex encoder and decoder. 
  10.  *  
  11.  */ 
  12. public class Hex implements BinaryEncoder, BinaryDecoder { 
  13.  
  14.     /**  
  15.      * Used building output as Hex  
  16.      */ 
  17.     private static final char[] DIGITS = { 
  18.         '0''1''2''3''4''5''6''7'
  19.            '8''9''a''b''c''d''e''f' 
  20.     }; 
  21.  
  22.     /** 
  23.      * Converts an array of characters representing hexidecimal values into an 
  24.      * array of bytes of those same values. The returned array will be half the 
  25.      * length of the passed array, as it takes two characters to represent any 
  26.      * given byte. An exception is thrown if the passed char array has an odd 
  27.      * number of elements. 
  28.      *  
  29.      * @param data An array of characters containing hexidecimal digits 
  30.      * @return A byte array containing binary data decoded from 
  31.      *         the supplied char array. 
  32.      * @throws DecoderException Thrown if an odd number or illegal of characters  
  33.      *         is supplied 
  34.      */ 
  35.     public static byte[] decodeHex(char[] data) throws DecoderException { 
  36.  
  37.         int len = data.length; 
  38.  
  39.         if ((len & 0x01) != 0) { 
  40.             throw new DecoderException("Odd number of characters."); 
  41.         } 
  42.  
  43.         byte[] out = new byte[len >> 1]; 
  44.  
  45.         // two characters form the hex value. 
  46.         for (int i = 0, j = 0; j < len; i++) { 
  47.             int f = toDigit(data[j], j) << 4
  48.             j++; 
  49.             f = f | toDigit(data[j], j); 
  50.             j++; 
  51.             out[i] = (byte) (f & 0xFF); 
  52.         } 
  53.  
  54.         return out; 
  55.     } 
  56.  
  57.     /** 
  58.      * Converts a hexadecimal character to an integer. 
  59.      *   
  60.      * @param ch A character to convert to an integer digit 
  61.      * @param index The index of the character in the source 
  62.      * @return An integer 
  63.      * @throws DecoderException Thrown if ch is an illegal hex character 
  64.      */ 
  65.     protected static int toDigit(char ch, int index) throws DecoderException { 
  66.         int digit = Character.digit(ch, 16); 
  67.         if (digit == -1) { 
  68.             throw new DecoderException("Illegal hexadecimal charcter " + ch + " at index " + index); 
  69.         } 
  70.         return digit; 
  71.     } 
  72.  
  73.     /** 
  74.      * Converts an array of bytes into an array of characters representing the hexidecimal values of each byte in order. 
  75.      * The returned array will be double the length of the passed array, as it takes two characters to represent any 
  76.      * given byte. 
  77.      *  
  78.      * @param data 
  79.      *                  a byte[] to convert to Hex characters 
  80.      * @return A char[] containing hexidecimal characters 
  81.      */ 
  82.     public static char[] encodeHex(byte[] data) { 
  83.  
  84.         int l = data.length; 
  85.  
  86.            char[] out = new char[l << 1]; 
  87.  
  88.            // two characters form the hex value. 
  89.            for (int i = 0, j = 0; i < l; i++) { 
  90.                out[j++] = DIGITS[(0xF0 & data[i]) >>> 4 ]; 
  91.                out[j++] = DIGITS[ 0x0F & data[i] ]; 
  92.            } 
  93.  
  94.            return out; 
  95.     } 
  96.      
  97.     /** 
  98.      * Converts an array of character bytes representing hexidecimal values into an 
  99.      * array of bytes of those same values. The returned array will be half the 
  100.      * length of the passed array, as it takes two characters to represent any 
  101.      * given byte. An exception is thrown if the passed char array has an odd 
  102.      * number of elements. 
  103.      *  
  104.      * @param array An array of character bytes containing hexidecimal digits 
  105.      * @return A byte array containing binary data decoded from 
  106.      *         the supplied byte array (representing characters). 
  107.      * @throws DecoderException Thrown if an odd number of characters is supplied 
  108.      *                   to this function 
  109.      * @see #decodeHex(char[]) 
  110.      */ 
  111.     public byte[] decode(byte[] array) throws DecoderException { 
  112.         return decodeHex(new String(array).toCharArray()); 
  113.     } 
  114.      
  115.     /** 
  116.      * Converts a String or an array of character bytes representing hexidecimal values into an 
  117.      * array of bytes of those same values. The returned array will be half the 
  118.      * length of the passed String or array, as it takes two characters to represent any 
  119.      * given byte. An exception is thrown if the passed char array has an odd 
  120.      * number of elements. 
  121.      *  
  122.      * @param object A String or, an array of character bytes containing hexidecimal digits 
  123.      * @return A byte array containing binary data decoded from 
  124.      *         the supplied byte array (representing characters). 
  125.      * @throws DecoderException Thrown if an odd number of characters is supplied 
  126.      *                   to this function or the object is not a String or char[] 
  127.      * @see #decodeHex(char[]) 
  128.      */ 
  129.     public Object decode(Object object) throws DecoderException { 
  130.         try { 
  131.             char[] charArray = object instanceof String ? ((String) object).toCharArray() : (char[]) object; 
  132.             return decodeHex(charArray); 
  133.         } catch (ClassCastException e) { 
  134.             throw new DecoderException(e.getMessage()); 
  135.         } 
  136.     } 
  137.      
  138.     /** 
  139.      * Converts an array of bytes into an array of bytes for the characters representing the 
  140.      * hexidecimal values of each byte in order. The returned array will be 
  141.      * double the length of the passed array, as it takes two characters to 
  142.      * represent any given byte. 
  143.      * 
  144.      * @param array a byte[] to convert to Hex characters 
  145.      * @return A byte[] containing the bytes of the hexidecimal characters 
  146.      * @see #encodeHex(byte[]) 
  147.      */ 
  148.     public byte[] encode(byte[] array) { 
  149.         return new String(encodeHex(array)).getBytes(); 
  150.     } 
  151.  
  152.     /** 
  153.      * Converts a String or an array of bytes into an array of characters representing the 
  154.      * hexidecimal values of each byte in order. The returned array will be 
  155.      * double the length of the passed String or array, as it takes two characters to 
  156.      * represent any given byte. 
  157.      * 
  158.      * @param object a String, or byte[] to convert to Hex characters 
  159.      * @return A char[] containing hexidecimal characters 
  160.      * @throws EncoderException Thrown if the given object is not a String or byte[] 
  161.      * @see #encodeHex(byte[]) 
  162.      */ 
  163.     public Object encode(Object object) throws EncoderException {     
  164.         try { 
  165.             byte[] byteArray = object instanceof String ? ((String) object).getBytes() : (byte[]) object; 
  166.             return encodeHex(byteArray); 
  167.         } catch (ClassCastException e) { 
  168.             throw new EncoderException(e.getMessage()); 
  169.         } 
  170.     } 
  171.  

更多的移动互联网的发展趋势、app开发、移动互联网应用相关的资料请到互联网的一点事:www.yidin.net 留言

android QQ群:222392467

资料:

http://www.yidin.net/?p=8280

http://www.yidin.net/?p=9725

推荐阅读:
  1. 好程序员Java教程分享Java之包装类与常用类
  2. 如何通过封装一个v-clamp的指令处理多行文本溢出

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

分享 ers

上一篇:centos7安装配置pgAgent

下一篇:10年老司机告诉你,怎选光缆通讯配件?

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》