gocha

HexDump.java - SJIS日本語ダンプ付き

Jul 10th, 2013
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.30 KB | None | 0 0
  1. /*
  2.  * Java HexDump class modified by gocha.
  3.  * Original: http://code.google.com/p/google-tv-pairing-protocol/source/browse/java/src/com/google/polo/pairing/HexDump.java
  4.  */
  5.  
  6. /*
  7.  * Copyright (C) 2006 The Android Open Source Project
  8.  *
  9.  * Licensed under the Apache License, Version 2.0 (the "License");
  10.  * you may not use this file except in compliance with the License.
  11.  * You may obtain a copy of the License at
  12.  *
  13.  *      http://www.apache.org/licenses/LICENSE-2.0
  14.  *
  15.  * Unless required by applicable law or agreed to in writing, software
  16.  * distributed under the License is distributed on an "AS IS" BASIS,
  17.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18.  * See the License for the specific language governing permissions and
  19.  * limitations under the License.
  20.  */
  21.  
  22. import java.io.UnsupportedEncodingException;
  23.  
  24. public class HexDump
  25. {
  26.     private final static char[] HEX_DIGITS = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
  27.    
  28.     public static String dumpHexString(byte[] array)
  29.     {
  30.         return dumpHexString(array, 0, array.length);
  31.     }
  32.    
  33.     public static String dumpHexString(byte[] array, int offset, int length)
  34.     {
  35.         StringBuilder result = new StringBuilder();
  36.        
  37.         byte[] line = new byte[16];
  38.         int lineIndex = 0;
  39.         boolean firstLine = true;
  40.         boolean lastLineEndsWithDBCS = false;
  41.        
  42.         for (int i = offset ; i < offset + length ; i++)
  43.         {
  44.             if (lineIndex == 0)
  45.             {
  46.                 if (!firstLine)
  47.                 {
  48.                     result.append("\n");
  49.                 }
  50.                 result.append(toHexString(i));
  51.                 result.append(":");
  52.  
  53.                 firstLine = false;
  54.             }
  55.            
  56.             byte b = array[i];
  57.             result.append(" ");
  58.             result.append(HEX_DIGITS[(b >>> 4) & 0x0F]);
  59.             result.append(HEX_DIGITS[b & 0x0F]);
  60.            
  61.             line[lineIndex++] = b;
  62.            
  63.             if (lineIndex == 16)
  64.             {
  65.                 result.append(" ");
  66.                
  67.                 for (int j = 0 ; j < 16 ; j++)
  68.                 {
  69.                     boolean isDBCS = false;
  70.                     byte[] aDBCS = { line[j], (j != 15) ? line[j + 1] : array[i + 1] };
  71.                     String strDBCS = "";
  72.                     if (isWin31JDBCSByte(aDBCS[0], aDBCS[1]))
  73.                     {
  74.                         try
  75.                         {
  76.                             strDBCS = new String(aDBCS, "Windows-31J");
  77.                             isDBCS = true;
  78.                         }
  79.                         catch (UnsupportedEncodingException e) {}
  80.                     }
  81.                    
  82.                     if (j == 0 && lastLineEndsWithDBCS)
  83.                     {
  84.                         result.append(" ");
  85.                         lastLineEndsWithDBCS = false;
  86.                     }
  87.                     else if (isDBCS)
  88.                     {
  89.                         result.append(strDBCS);
  90.                         lastLineEndsWithDBCS = (j == 15);
  91.                         j++;
  92.                     }
  93.                     else
  94.                     {
  95.                         if (line[j] >= ' ' && line[j] <= '~')
  96.                         {
  97.                             result.append(new String(line, j, 1));
  98.                         }
  99.                         else
  100.                         {
  101.                             result.append(".");
  102.                         }
  103.                         lastLineEndsWithDBCS = false;
  104.                     }
  105.                 }
  106.                
  107.                 lineIndex = 0;
  108.             }
  109.         }
  110.        
  111.         if (lineIndex != 0)
  112.         {
  113.             int count = (16 - lineIndex) * 3;
  114.             count++;
  115.             for (int i = 0 ; i < count ; i++)
  116.             {
  117.                 result.append(" ");
  118.             }
  119.            
  120.             for (int i = 0 ; i < lineIndex ; i++)
  121.             {
  122.                 boolean isDBCS = false;
  123.                 byte[] aDBCS = { line[i], (i != 15) ? line[i + 1] : 0 };
  124.                 String strDBCS = "";
  125.                 if (isWin31JDBCSByte(aDBCS[0], aDBCS[1]))
  126.                 {
  127.                     try
  128.                     {
  129.                         strDBCS = new String(aDBCS, "Windows-31J");
  130.                         isDBCS = true;
  131.                     }
  132.                     catch (UnsupportedEncodingException e) {}
  133.                 }
  134.  
  135.                 if (i == 0 && lastLineEndsWithDBCS)
  136.                 {
  137.                     result.append(" ");
  138.                 }
  139.                 else if (isDBCS)
  140.                 {
  141.                     result.append(strDBCS);
  142.                     i++;
  143.                 }
  144.                 else
  145.                 {
  146.                     if (line[i] >= ' ' && line[i] <= '~')
  147.                     {
  148.                         result.append(new String(line, i, 1));
  149.                     }
  150.                     else
  151.                     {
  152.                         result.append(".");
  153.                     }
  154.                 }
  155.             }
  156.         }
  157.        
  158.         return result.toString();
  159.     }
  160.    
  161.     public static String toHexString(byte b)
  162.     {
  163.         return toHexString(toByteArray(b));
  164.     }
  165.  
  166.     public static String toHexString(byte[] array)
  167.     {
  168.         return toHexString(array, 0, array.length);
  169.     }
  170.    
  171.     public static String toHexString(byte[] array, int offset, int length)
  172.     {
  173.         char[] buf = new char[length * 2];
  174.  
  175.         int bufIndex = 0;
  176.         for (int i = offset ; i < offset + length; i++)
  177.         {
  178.             byte b = array[i];
  179.             buf[bufIndex++] = HEX_DIGITS[(b >>> 4) & 0x0F];
  180.             buf[bufIndex++] = HEX_DIGITS[b & 0x0F];
  181.         }
  182.  
  183.         return new String(buf);        
  184.     }
  185.    
  186.     public static String toHexString(int i)
  187.     {
  188.         return toHexString(toByteArray(i));
  189.     }
  190.    
  191.     public static byte[] toByteArray(byte b)
  192.     {
  193.         byte[] array = new byte[1];
  194.         array[0] = b;
  195.         return array;
  196.     }
  197.    
  198.     public static byte[] toByteArray(int i)
  199.     {
  200.         byte[] array = new byte[4];
  201.        
  202.         array[3] = (byte)(i & 0xFF);
  203.         array[2] = (byte)((i >> 8) & 0xFF);
  204.         array[1] = (byte)((i >> 16) & 0xFF);
  205.         array[0] = (byte)((i >> 24) & 0xFF);
  206.        
  207.         return array;
  208.     }
  209.    
  210.     private static int toByte(char c)
  211.     {
  212.         if (c >= '0' && c <= '9') return (c - '0');
  213.         if (c >= 'A' && c <= 'F') return (c - 'A' + 10);
  214.         if (c >= 'a' && c <= 'f') return (c - 'a' + 10);
  215.  
  216.         throw new RuntimeException ("Invalid hex char '" + c + "'");
  217.     }
  218.    
  219.     public static byte[] hexStringToByteArray(String hexString)
  220.     {
  221.         int length = hexString.length();
  222.         byte[] buffer = new byte[length / 2];
  223.  
  224.         for (int i = 0 ; i < length ; i += 2)
  225.         {
  226.             buffer[i / 2] = (byte)((toByte(hexString.charAt(i)) << 4) | toByte(hexString.charAt(i+1)));
  227.         }
  228.        
  229.         return buffer;
  230.     }
  231.  
  232.     private static boolean isWin31JDBCSByte(byte b1, byte b2)
  233.     {
  234.         int i1 = (b1 & 0xff);
  235.         int i2 = (b2 & 0xff);
  236.         return ((i1 >= 0x81 && i1 <= 0x9f) || (i1 >= 0xe0 && i1 <= 0xfc)) && (i2 >= 0x40 && i2 <= 0xfc);
  237.     }
  238. }
Add Comment
Please, Sign In to add comment