PageRenderTime 121ms CodeModel.GetById 20ms RepoModel.GetById 0ms 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
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 4998 2004-03-20 06:08:50Z spestov $
  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. if(target.length() == 0)
  61. return;
  62. try
  63. {
  64. byte b = Byte.parseByte(target, 16);
  65. String str = new String(new byte[] { b });
  66. StringBuffer foo = new StringBuffer();
  67. foo.append("" + target + " = " + str);
  68. print(foo.toString());
  69. }
  70. catch(NumberFormatException nfe)
  71. {
  72. Macros.error(view, "" + target + " is not valid hex string.");
  73. }
  74. }
  75. String target = Macros.input(view,"Byte to String:");
  76. if(target != null)
  77. byteToString(target);
  78. /*
  79. Macro index data (in DocBook format)
  80. <listitem>
  81. <para><filename>Hex_Convert.bsh</filename></para>
  82. <abstract><para>
  83. Converts byte characters to their hex equivalent, and vice versa.
  84. </para></abstract>
  85. </listitem>
  86. */
  87. // end Hex_Convert.bsh