OpenCores
URL https://opencores.org/ocsvn/present/present/trunk

Subversion Repositories present

[/] [present/] [trunk/] [JavaTests/] [PresentCommTesting/] [src/] [pl/] [com/] [kgajewski/] [serialcomm/] [datagen/] [PresentDataGenerator.java] - Blame information for rev 19

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 3 gajos
package pl.com.kgajewski.serialcomm.datagen;
2
import java.io.BufferedWriter;
3
import java.io.File;
4
import java.io.FileWriter;
5
import java.io.IOException;
6
 
7
import org.apache.commons.lang3.StringUtils;
8
 
9 19 gajos
//Some class preparing data for communication testing
10
//It can be easy used from Eclipse iDE
11
//Only change the "drive" variable where to write testfiles.
12 3 gajos
 
13 19 gajos
 
14 3 gajos
public class PresentDataGenerator {
15
        public static void main(String[] args) {
16
                String drive = "e:\\";
17
                String data = "a112ffc72f68417b";
18
                String key  = "00000000000000000000";
19
 
20
                String data2 = "3333dcd3213210d2";
21
                String key2  = "ffffffffffffffffffff";
22
 
23
                try {
24
                        System.out.println("key");
25
                        File f1 = new File(drive + "key.txt");
26
                        f1.createNewFile();
27
                        formatDataFromHex(key, f1);
28
 
29
                        System.out.println("data");
30
                        File f2 = new File(drive + "data.txt");
31
                        f1.createNewFile();
32
                        formatDataFromHex(data, f2);
33
 
34
                        System.out.println("key2");
35
                        File f3 = new File(drive + "key2.txt");
36
                        f3.createNewFile();
37
                        formatDataFromHex(key2, f3);
38
 
39
                        System.out.println("data2");
40
                        File f4 = new File(drive + "data2.txt");
41
                        f4.createNewFile();
42
                        formatDataFromHex(data2, f4);
43
 
44
                } catch (IOException e1) {
45
                        // TODO Auto-generated catch block
46
                        e1.printStackTrace();
47
                }
48
 
49
        }
50
 
51
        private static void formatDataFromHex(String str, File f) throws IOException {
52
                BufferedWriter bfw = new BufferedWriter(new FileWriter(f));
53
                for (int i = str.length(); i > 0; i -= 2) {
54
                        String substr = str.substring(i - 2, i);
55
 
56
                        parseByteStringHex(bfw, substr);
57
                }
58
                bfw.close();
59
        }
60
 
61
        private static void parseByteStringHex(BufferedWriter bfw, String str)
62
                        throws IOException {
63
                Integer i = Integer.valueOf(str, 16);
64
                String s = Integer.toString(i, 2);
65
                String tmp = "";
66
                for (int j = 8 - s.length(); j > 0; j--) {
67
                        tmp = tmp.concat("0");
68
                }
69
                parseByteString(bfw, tmp + s);
70
        }
71
 
72
        private static void parseByteString(BufferedWriter bfw, String str)
73
                        throws IOException {
74
                int ones = 0;
75
                bfw.write(str);
76
                bfw.write("\n");
77
                str = StringUtils.reverse(str);
78
 
79
                for (int j = 0; j < str.length(); j++) {
80
                        bfw.write(str.charAt(j));
81
                        bfw.write("\n");
82
                        if (str.charAt(j) == '1') {
83
                                ones++;
84
                        }
85
                }
86
                if (ones % 2 == 1) {
87
                        bfw.write("0");
88
                } else {
89
                        bfw.write("1");
90
                }
91
                bfw.write("\n");
92
        }
93
 
94
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.