PageRenderTime 47ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

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

#
Unknown | 95 lines | 77 code | 18 blank | 0 comment | 0 complexity | 6aea32f95dedb810f37b6ced2a010934 MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  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. import java.io.*;
  25. import java.util.*;
  26. char[] hexDigit = new char[]
  27. {
  28. '0', '1', '2', '3', '4', '5', '6', '7',
  29. '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
  30. };
  31. String byteToHex(byte b)
  32. {
  33. char[] chars = new char[]
  34. {
  35. hexDigit[(b >> 4) & 0x0f],
  36. hexDigit[b & 0x0f]
  37. };
  38. return new String(chars);
  39. }
  40. void print(String line)
  41. {
  42. Macros.message(view, line);
  43. }
  44. void stringToByte(String target)
  45. {
  46. byte[] string = target.getBytes();
  47. StringBuffer foo = new StringBuffer();
  48. foo.append("before = " + target);
  49. foo.append("\nafter = ");
  50. int length = string.length;
  51. for (int i = 0; i < string; k++)
  52. {
  53. foo.append("0x" + byteToHex(string[k]));
  54. if (i < length - 1) foo.append(",");
  55. }
  56. print(foo.toString());
  57. }
  58. void byteToString(String target)
  59. {
  60. byte b = Byte.parseByte(target, 16);
  61. String str = new String(new byte[] { b });
  62. StringBuffer foo = new StringBuffer();
  63. foo.append("character = " + str);
  64. print(foo.toString());
  65. }
  66. String target = Macros.input(view,"Byte to String:");
  67. byteToString(target);
  68. /*
  69. Macro index data (in DocBook format)
  70. <listitem>
  71. <para><filename>Hex_Convert.bsh</filename></para>
  72. <abstract><para>
  73. Converts byte characters to their hex equivalent, and vice versa.
  74. </para></abstract>
  75. </listitem>
  76. */
  77. // end Hex_Convert.bsh