fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. /* */ public static String toHexString(byte[] bytes) {
  11. /* 215 */ StringBuilder hexString = new StringBuilder();
  12. /* 216 */ for (int i = 0; i < bytes.length; ++i) {
  13. /* 217 */ hexString.append(Integer.toHexString(0xFF & bytes[i]));
  14. /* */ }
  15. /* 219 */ return hexString.toString();
  16. /* */ }
  17.  
  18. private static String toHex(byte[] checksumArray)
  19. {
  20. StringBuilder result = new StringBuilder();
  21. for (byte checksumByte : checksumArray)
  22. {
  23. String hex = Integer.toString((checksumByte & 0xff) + 0x100, 16).substring(1);
  24. result.append(hex);
  25. }
  26. return result.toString();
  27. }
  28.  
  29. private static final char[] hexchars = "0123456789abcdef".toCharArray();
  30.  
  31. private static String toHexRL(byte[] checksumArray) {
  32. int len = checksumArray.length * 2;
  33. char[] hchars = new char[len];
  34. int pos = 0;
  35. for (byte b : checksumArray) {
  36. hchars[pos++] = hexchars[(b>>4) & 0xf];
  37. hchars[pos++] = hexchars[b & 0xf];
  38. }
  39. return new String(hchars);
  40. }
  41.  
  42. public static void main(String[] args) {
  43. byte[][] testcases = new byte[][]{
  44. {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}
  45. };
  46. for (byte[] tc : testcases) {
  47. System.out.printf("Input %s\n CXF %s\n OP %s\n RL %s", Arrays.toString(tc), toHexString(tc), toHex(tc), toHexRL(tc));
  48. }
  49. }
  50. }
Success #stdin #stdout 0.12s 55968KB
stdin
Standard input is empty
stdout
Input [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
  CXF 123456789abcdef1011121314
  OP  0102030405060708090a0b0c0d0e0f1011121314
  RL  0102030405060708090a0b0c0d0e0f1011121314