1 package org.rpi.songcast;
2
3 public class SongcastMessage {
4
5 public byte[] data = null;
6
7 public String convertHexToString(String hex) {
8
9 StringBuilder sb = new StringBuilder();
10 StringBuilder temp = new StringBuilder();
11
12
13 for (int i = 0; i < hex.length() - 1; i += 2) {
14
15
16 String output = hex.substring(i, (i + 2));
17
18 int decimal = Integer.parseInt(output, 16);
19
20 sb.append((char) decimal);
21
22 temp.append(decimal);
23 }
24 System.out.println("Decimal : " + temp.toString());
25
26 return sb.toString();
27 }
28
29 public String DecToHex(int number, int length) {
30 StringBuilder sb = new StringBuilder();
31 sb.append(Integer.toHexString(number));
32 while (sb.length() < length) {
33 sb.insert(0, '0');
34 }
35 String hex = sb.toString();
36 return hex;
37 }
38
39 public byte[] hexStringToByteArray(String s) {
40 int len = s.length();
41 byte[] data = new byte[len / 2];
42 for (int i = 0; i < len; i += 2) {
43 data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) + Character.digit(s.charAt(i + 1), 16));
44 }
45 return data;
46 }
47
48 public String stringToHex(String string) {
49 StringBuilder buf = new StringBuilder(200);
50 for (char ch : string.toCharArray()) {
51
52
53 buf.append(String.format("%02x", (int) ch));
54 }
55 return buf.toString();
56 }
57
58
59
60
61
62 public byte[] getBytes(int start, int end)
63 {
64 int size = (end - start) + 1;
65 int count = 0;
66 byte[] res = new byte[size];
67 for(int i = start;i<=end;i++)
68 {
69 res[count] = data[i];
70 count++;
71 }
72 return res;
73 }
74
75 public int byteArrayToInt(byte[] b)
76 {
77 int value = 0;
78 for (int i = 0; i < 4; i++) {
79 int shift = (4 - 1 - i) * 8;
80 value += (b[i] & 0x000000FF) << shift;
81 }
82 return value;
83 }
84
85 public String byteArrayToString(byte[] bytes)
86 {
87 StringBuilder sb = new StringBuilder();
88 for (byte b : bytes) {
89 sb.append(String.format("%02X ", b));
90 }
91 return sb.toString();
92 }
93
94 }