PageRenderTime 75ms CodeModel.GetById 71ms app.highlight 1ms RepoModel.GetById 2ms app.codeStats 0ms

/jEdit/tags/jedit-4-2-pre14/macros/Misc/Hex_Convert.bsh

#
Unknown | 107 lines | 87 code | 20 blank | 0 comment | 0 complexity | c95eb801f3f50be3549b9ca604a22923 MD5 | raw file
  1/*
  2 * Hex_Convert.bsh - a BeanShell macro script for the
  3 * jEdit text editor - Converts byte characters to their
  4 * hex equivalent, and vice versa.
  5 * Copyright (C) 2001 Will Sargent
  6 * will_sargent@yahoo.dom
  7 *
  8 * This program is free software; you can redistribute it and/or
  9 * modify it under the terms of the GNU General Public License
 10 * as published by the Free Software Foundation; either version 2
 11 * of the License, or any later version.
 12 *
 13 * This program is distributed in the hope that it will be useful,
 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 16 * GNU General Public License for more details.
 17 *
 18 * You should have received a copy of the GNU General Public License
 19 * along with the jEdit program; if not, write to the Free Software
 20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 21 *
 22 * $Id: Hex_Convert.bsh 4998 2004-03-20 06:08:50Z spestov $
 23 */
 24
 25import java.io.*;
 26import java.util.*;
 27
 28char[] hexDigit = new char[]
 29{
 30    '0', '1', '2', '3', '4', '5', '6', '7',
 31    '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
 32};
 33
 34String byteToHex(byte b)
 35{
 36    char[] chars = new char[]
 37    {
 38        hexDigit[(b >> 4) & 0x0f],
 39        hexDigit[b & 0x0f]
 40    };
 41    return new String(chars);
 42}
 43
 44void print(String line)
 45{
 46    Macros.message(view, line);
 47}
 48
 49void stringToByte(String target)
 50{
 51    byte[] string = target.getBytes();
 52
 53    StringBuffer foo = new StringBuffer();
 54    foo.append("before = " + target);
 55    foo.append("\nafter = ");
 56
 57    int length = string.length;
 58    for (int i = 0; i < string; k++)
 59    {
 60		
 61        foo.append("0x" + byteToHex(string[k]));
 62        if (i < length - 1) foo.append(",");
 63    }
 64    print(foo.toString());
 65}
 66
 67void byteToString(String target)
 68{
 69	if(target.length() == 0)
 70		return;
 71
 72	try
 73	{
 74    byte b = Byte.parseByte(target, 16);
 75    String str = new String(new byte[] { b });
 76
 77    StringBuffer foo = new StringBuffer();
 78		foo.append("" + target + " = " + str);
 79
 80    print(foo.toString());
 81	}
 82	catch(NumberFormatException nfe)
 83	{
 84		Macros.error(view, "" + target + " is not valid hex string.");
 85	}
 86}
 87
 88String target = Macros.input(view,"Byte to String:");
 89if(target != null)
 90	byteToString(target);
 91
 92
 93/*
 94
 95Macro index data (in DocBook format)
 96
 97  <listitem>
 98    <para><filename>Hex_Convert.bsh</filename></para>
 99    <abstract><para>
100      Converts byte characters to their hex equivalent, and vice versa.
101    </para></abstract>
102  </listitem>
103
104*/
105
106// end Hex_Convert.bsh
107