PageRenderTime 41ms CodeModel.GetById 20ms app.highlight 1ms RepoModel.GetById 19ms app.codeStats 0ms

/jEdit/tags/jedit-4-0-pre3/macros/Misc/Hex_Convert.bsh

#
Unknown | 95 lines | 77 code | 18 blank | 0 comment | 0 complexity | 6aea32f95dedb810f37b6ced2a010934 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 3912 2001-11-24 03:56:51Z jgellene $
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        foo.append("0x" + byteToHex(string[k]));
61        if (i < length - 1) foo.append(",");
62    }
63    print(foo.toString());
64}
65
66void byteToString(String target)
67{
68    byte b = Byte.parseByte(target, 16);
69    String str = new String(new byte[] { b });
70
71    StringBuffer foo = new StringBuffer();
72    foo.append("character = " + str);
73
74    print(foo.toString());
75}
76
77String target = Macros.input(view,"Byte to String:");
78byteToString(target);
79
80
81/*
82
83Macro index data (in DocBook format)
84
85  <listitem>
86    <para><filename>Hex_Convert.bsh</filename></para>
87    <abstract><para>
88      Converts byte characters to their hex equivalent, and vice versa.
89    </para></abstract>
90  </listitem>
91
92*/
93
94// end Hex_Convert.bsh
95