zoukankan      html  css  js  c++  java
  • HexDump.java解析

    从包名我们能够看出该类并没有相应用开发人员开放,也就是说在google开放的Android API文档中并没有该类的相关介绍。好在Android系统源代码是开源的,因此我在解决framework中问题的时候发现了这个工具类。因为工作原因一直没有回头在细细的领会这个工具类的精髓,因此今天花点时间对这个类进行全面的分析一遍。以备不时之需。

    首先分析之前。我们须要了解该类使用来进行16禁止转换的。我之前也写过相关的工具类,可是去了解Google的精神还是有必要的。


    package com.android.internal.util;


    public class HexDump
    {
        private final static char[] HEX_DIGITS = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };//十六进制的组成元素
        
        public static String dumpHexString(byte[] array)
        {
            return dumpHexString(array, 0, array.length);
        }
        
        public static String dumpHexString(byte[] array, int offset, int length)
        {
            StringBuilder result = new StringBuilder();
            
            byte[] line = new byte[16];
            int lineIndex = 0;
            
            result.append(" 0x");
            result.append(toHexString(offset));
            
            for (int i = offset ; i < offset + length ; i++)
            {
                if (lineIndex == 16)
                {
                    result.append(" ");
                    
                    for (int j = 0 ; j < 16 ; j++)
                    {
                        if (line[j] > ' ' && line[j] < '~')
                        {
                            result.append(new String(line, j, 1));
                        }
                        else
                        {
                            result.append(".");
                        }
                    }
                    
                    result.append(" 0x");
                    result.append(toHexString(i));
                    lineIndex = 0;
                }
                
                byte b = array[i];
                result.append(" ");
                result.append(HEX_DIGITS[(b >>> 4) & 0x0F]);
                result.append(HEX_DIGITS[b & 0x0F]);
                
                line[lineIndex++] = b;
            }
            
            if (lineIndex != 16)
            {
                int count = (16 - lineIndex) * 3;
                count++;
                for (int i = 0 ; i < count ; i++)
                {
                    result.append(" ");
                }
                
                for (int i = 0 ; i < lineIndex ; i++)
                {
                    if (line[i] > ' ' && line[i] < '~')
                    {
                        result.append(new String(line, i, 1));
                    }
                    else
                    {
                        result.append(".");
                    }
                }
            }
            
            return result.toString();
        }
        
        public static String toHexString(byte b)
        {
            return toHexString(toByteArray(b));
        }

        public static String toHexString(byte[] array)
        {
            return toHexString(array, 0, array.length);
        }
        
        public static String toHexString(byte[] array, int offset, int length)
        {
            char[] buf = new char[length * 2];

            int bufIndex = 0;
            for (int i = offset ; i < offset + length; i++)
            {
                byte b = array[i];
                buf[bufIndex++] = HEX_DIGITS[(b >>> 4) & 0x0F];
                buf[bufIndex++] = HEX_DIGITS[b & 0x0F];
            }

            return new String(buf);        
        }
        
        public static String toHexString(int i)
        {
            return toHexString(toByteArray(i));
        }
        
        public static byte[] toByteArray(byte b)
        {
            byte[] array = new byte[1];
            array[0] = b;
            return array;
        }
        
        public static byte[] toByteArray(int i)
        {
            byte[] array = new byte[4];
            
            array[3] = (byte)(i & 0xFF);
            array[2] = (byte)((i >> 8) & 0xFF);
            array[1] = (byte)((i >> 16) & 0xFF);
            array[0] = (byte)((i >> 24) & 0xFF);
            
            return array;
        }
        
        private static int toByte(char c)
        {
            if (c >= '0' && c <= '9') return (c - '0');
            if (c >= 'A' && c <= 'F') return (c - 'A' + 10);
            if (c >= 'a' && c <= 'f') return (c - 'a' + 10);

            throw new RuntimeException ("Invalid hex char '" + c + "'");
        }
        
        public static byte[] hexStringToByteArray(String hexString)
        {
            int length = hexString.length();
            byte[] buffer = new byte[length / 2];

            for (int i = 0 ; i < length ; i += 2)
            {
                buffer[i / 2] = (byte)((toByte(hexString.charAt(i)) << 4) | toByte(hexString.charAt(i+1)));
            }
            
            return buffer;
        }    
    }


  • 相关阅读:
    相遇相知都是缘
    [BTS]6912,5641,5773,5410错误处理!
    [BTS]Cumulative Functloids用法
    职业生涯又一个转折点
    BizTalk Group Day 北京!
    [BTS]10008错误如何处理?
    [ERROR]创建BAM视图时报错
    [BTS]使用BizTalk开发应用系统,就是这么简单!
    [BTS]BizTalk2006 SqlAdapter UpdateGram的Update用法
    [BTS]为什么BTSharePointAdapterWS.asmx无法预览?
  • 原文地址:https://www.cnblogs.com/yjbjingcha/p/6929434.html
Copyright © 2011-2022 走看看