1. /*
  2. * @(#)file SnmpTools.java
  3. * @(#)author Sun Microsystems, Inc.
  4. * @(#)version 1.15
  5. * @(#)date 04/09/15
  6. *
  7. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  8. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  9. *
  10. */
  11. package com.sun.jmx.snmp.internal;
  12. import com.sun.jmx.snmp.SnmpDefinitions;
  13. /**
  14. * Utility class used to deal with various data representations.
  15. * <p><b>This API is a Sun Microsystems internal API and is subject
  16. * to change without notice.</b></p>
  17. * @since 1.5
  18. */
  19. public class SnmpTools implements SnmpDefinitions {
  20. /**
  21. * Translates a binary representation in an ASCII one. The returned string is an hexadecimal string starting with 0x.
  22. * @param data Binary to translate.
  23. * @return Translated binary.
  24. */
  25. static public String binary2ascii(byte[] data, int length)
  26. {
  27. if(data == null) return null;
  28. final int size = (length * 2) + 2;
  29. byte[] asciiData = new byte[size];
  30. asciiData[0] = (byte) '0';
  31. asciiData[1] = (byte) 'x';
  32. for (int i=0; i < length; i++) {
  33. int j = i*2;
  34. int v = (data[i] & 0xf0);
  35. v = v >> 4;
  36. if (v < 10)
  37. asciiData[j+2] = (byte) ('0' + v);
  38. else
  39. asciiData[j+2] = (byte) ('A' + (v - 10));
  40. v = ((data[i] & 0xf));
  41. if (v < 10)
  42. asciiData[j+1+2] = (byte) ('0' + v);
  43. else
  44. asciiData[j+1+2] = (byte) ('A' + (v - 10));
  45. }
  46. return new String(asciiData);
  47. }
  48. /**
  49. * Translates a binary representation in an ASCII one. The returned string is an hexadecimal string starting with 0x.
  50. * @param data Binary to translate.
  51. * @return Translated binary.
  52. */
  53. static public String binary2ascii(byte[] data)
  54. {
  55. return binary2ascii(data, data.length);
  56. }
  57. /**
  58. * Translates a stringified representation in a binary one. The passed string is an hexadecimal one starting with 0x.
  59. * @param str String to translate.
  60. * @return Translated string.
  61. */
  62. static public byte[] ascii2binary(String str) {
  63. if(str == null) return null;
  64. String val = str.substring(2);
  65. int size = val.length();
  66. byte []buf = new byte[size2];
  67. byte []p = val.getBytes();
  68. for(int i = 0; i < (int) (size / 2); i++)
  69. {
  70. int j = i * 2;
  71. byte v = 0;
  72. if (p[j] >= '0' && p[j] <= '9') {
  73. v = (byte) ((p[j] - '0') << 4);
  74. }
  75. else if (p[j] >= 'a' && p[j] <= 'f') {
  76. v = (byte) ((p[j] - 'a' + 10) << 4);
  77. }
  78. else if (p[j] >= 'A' && p[j] <= 'F') {
  79. v = (byte) ((p[j] - 'A' + 10) << 4);
  80. }
  81. else
  82. throw new Error("BAD format :" + str);
  83. if (p[j+1] >= '0' && p[j+1] <= '9') {
  84. //System.out.println("ascii : " + p[j+1]);
  85. v += (p[j+1] - '0');
  86. //System.out.println("binary : " + v);
  87. }
  88. else if (p[j+1] >= 'a' && p[j+1] <= 'f') {
  89. //System.out.println("ascii : " + p[j+1]);
  90. v += (p[j+1] - 'a' + 10);
  91. //System.out.println("binary : " + v+1);
  92. }
  93. else if (p[j+1] >= 'A' && p[j+1] <= 'F') {
  94. //System.out.println("ascii : " + p[j+1]);
  95. v += (p[j+1] - 'A' + 10);
  96. //System.out.println("binary : " + v);
  97. }
  98. else
  99. throw new Error("BAD format :" + str);
  100. buf[i] = (byte) v;
  101. }
  102. return buf;
  103. }
  104. }