Source code: plugins/IcqEngine/protocol/ByteEncoder.java
1 /*
2 * IntEncoder.java
3 *
4 * Created on October 31, 2002, 6:03 PM
5 */
6
7 package plugins.IcqEngine.protocol;
8
9 import java.util.StringTokenizer;
10 /**
11 *
12 * @author Tobias Riemer
13 */
14 public class ByteEncoder {
15
16 /** Creates a new instance of IntEncoder */
17 public ByteEncoder() {
18 }
19
20 static public byte [] encode(int i,int len) {
21 byte [] b = new byte[len];
22 for (int j=0;j<len;j++) {
23 b[j] = (byte) (i % 256); //(new Integer(i)).byteValue();
24 //System.out.println("b: " + b[j] + " i: " + i);
25 i=i>>8;
26 }
27 return b;
28 }
29
30 static public byte [] encode(String str) {
31
32 byte[] b = new byte[str.length()+1];
33
34 for (int i = 0; i < str.length(); i++) {
35 b[i] = (byte)str.charAt(i);
36 }
37 b[str.length()] = 0;
38
39 return b;
40 }
41
42 static public byte [] encodeHex(String hex) {
43 StringTokenizer st = new StringTokenizer(hex);
44 byte[] b = new byte[st.countTokens()];
45
46 int i = 0;
47 while (st.hasMoreTokens()) {
48 String s = "0x" + st.nextToken();
49 int hi = (Integer.decode(s).intValue());
50 b[i] = (byte)hi;
51 i++;
52 }
53 return b;
54 }
55
56 static byte[] encodeIp(String ip) {
57 StringTokenizer st = new StringTokenizer(ip, ".");
58 byte[] b = new byte[4];
59 int i = 0;
60 while (st.hasMoreTokens()) {
61 int hi = (Integer.decode(st.nextToken()).intValue());
62 b[i] = (byte)hi;
63 i++;
64 }
65 return b;
66 }
67
68
69 public static void main(String[] args) throws Exception {
70 String t = "terst";
71 System.out.println(t.length() + " " + t.getBytes().length);
72 /*int i = ;
73 byte b1 = (byte) (i % 256);
74 byte b2 = (byte) ( (int)(i / 256) % 256);
75 byte b3 = (byte) ( (int)(i / 256/256) % 256);
76 byte b4 = (byte) ( (int)(i / 256/256/256) % 256);
77 System.out.println("");
78 System.out.print(b1);
79 System.out.print(b2);
80 System.out.print(b3);
81 System.out.print(b4);
82 System.out.println("");
83 byte [] b = new byte[4];
84 for (int j=3;j>=0;j--) {
85 System.out.print((new Integer(i)).byteValue());
86 b[j] = (new Integer(i)).byteValue();
87 i=i>>8;
88 }
89 i = 0;
90 System.out.println("");
91 byte [] bs = ByteEncoder.encode(i,4);
92 for (int j=0;j<4;j++) {
93 System.out.print((int)bs[j]);
94 }
95 System.out.println("");
96 System.out.println(ByteDecoder.decode(bs));
97 */
98 }
99 }