Chinaunix

标题: Dump Hex 功能 for jEdit [打印本页]

作者: pi1ot    时间: 2012-10-19 11:49
标题: Dump Hex 功能 for jEdit
因为对jEdit现有Hex相关功能插件(Hex、HexEdit、HexTools)功能不太满意,写了一个简单的macro提供Dump Hex功能,设置一个快捷键或者添加到右键菜单的话会更方便,效果见附图,另外jedit-cn讨论组链接为: http://t.cn/zl8AOWG



macro代码如下

  1. char[] hex_digit = new char[] {
  2.     '0', '1', '2', '3', '4', '5', '6', '7',
  3.     '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
  4. };

  5. String byte_to_hex( byte b ) {
  6.     char[] chars = new char[] {
  7.         hex_digit[ (b >> 4) & 0x0f ],
  8.         hex_digit[ b & 0x0f ]
  9.     };
  10.     return new String( chars );
  11. }

  12. String dump_hex( String target ) {
  13.     byte[] string = target.getBytes();
  14.     StringBuffer hex = new StringBuffer();

  15.     int tail = 0;
  16.     String src = "";
  17.     int length = string.length;
  18.         for ( int i=0; i<length; ++i ) {
  19.                
  20.                 char curr = string[i];
  21.                 if ( curr == '\r' || curr=='\n' || curr=='\t' ) {
  22.                         src += " ";
  23.                 } else {
  24.                         src += curr;
  25.                 }
  26.                
  27.                 hex.append( byte_to_hex(string[i]) + " " );
  28.                 if ( ((i+1)%19) == 0 ) {
  29.                         hex.append( " | " + src + "\n" );
  30.                         src = "";
  31.                         tail = 1;
  32.                 } else {
  33.                         tail = 0;
  34.                 }
  35.     }
  36.    
  37.     if ( tail==0 && src!="" ) {
  38.             int align = 19 - src.length();
  39.             String blank = "";
  40.             for ( int i=0; i<align; ++i ) {
  41.                     blank += "   ";
  42.             }
  43.             hex.append( blank + " | " + src );
  44.     }
  45.    
  46.     return hex.toString();
  47. }

  48. String text = textArea.getSelectedText();
  49. if ( text==null || text=="" ) {
  50.         textArea.selectAll();
  51.         text = textArea.getSelectedText();
  52.         textArea.selectNone();
  53. }
  54. if ( text != null ) {
  55.         String hex = dump_hex( text );
  56.         newbuf = jEdit.newFile( view );
  57.         newbuf.insert( 0, hex );
  58. }
复制代码

作者: linux_c_py_php    时间: 2012-10-19 20:03
谢谢分享~~~




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2