author | Christian Urban <urbanc@in.tum.de> |
Fri, 17 Mar 2017 12:15:58 +0000 | |
changeset 479 | 52aa298211f6 |
parent 145 | 920f675b4ed1 |
child 481 | acd8780bfc8b |
permissions | -rw-r--r-- |
479 | 1 |
// convert normal string to hex bytes string |
2 |
def string2hex(str: String): String = { |
|
3 |
str.toList.map(_.toInt.toHexString).mkString |
|
4 |
} |
|
5 |
||
6 |
// convert hex bytes string to normal string |
|
7 |
||
8 |
||
9 |
val byteArray: Array[Byte] = List.range(1, 255).map(_.toByte).toArray |
|
10 |
val bos = new BufferedOutputStream(new FileOutputStream("test.txt")) |
|
11 |
bos.write(byteArray) |
|
12 |
bos.close() |
|
13 |
||
14 |
||
15 |
def string2int(hex: String) = { |
|
16 |
hex.sliding(2, 2).map(Integer.parseInt(_, 16)).toArray |
|
17 |
} |
|
18 |
||
19 |
def test(l: List[Int]) = { |
|
20 |
l.map(_.toChar).mkString |
|
21 |
} |
|
22 |
||
23 |
||
24 |
import java.io._ |
|
25 |
val writer = new PrintWriter(new File("test.txt" )) |
|
26 |
||
27 |
//writer.write(string2int("cafebabe")) |
|
28 |
writer.write(test(List.range(1, 255))) |
|
29 |
writer.close() |
|
30 |
||
31 |
import scalax.io._ |
|
32 |
import scalax.io.Resource |
|
33 |
||
34 |
FileOutputStream fos = new FileOutputStream("test"); |
|
35 |
fos.write(bytesArray); |
|
36 |
fos.close(); |
|
37 |
||
38 |
||
39 |
string2int("cafebabe") |
|
40 |
||
41 |
202.toHexString |
|
42 |
||
43 |
"cafebabe".sliding(1, 1).toList.map(Integer.parseInt(_, 16)).map(_.toByte).map(_.toChar) |
|
44 |
||
45 |
hex2string("ca") |
|
46 |
string2hex("ca") |
|
47 |
hex2string("cafebabe") |
|
48 |
||
49 |
||
50 |
val appkey = "9GLV//lv/kYFW2o3/bihxwnMcmo=" |
|
51 |
||
52 |
// string to hex |
|
53 |
val appkey_hex = string2hex(appkey) |
|
54 |
// 39474c562f2f6c762f6b594657326f332f62696878776e4d636d6f3d |
|
55 |
println(appkey_hex) |
|
56 |
||
57 |
// hex to string |
|
58 |
val appkey_string_again = hex2string(appkey_hex) |
|
59 |
// 9GLV//lv/kYFW2o3/bihxwnMcmo= |
|
60 |
println(appkey_string_again) |
|
61 |
} |
|
62 |
||
63 |
||
64 |
List("ca", "fe", "ba", "be").map(_.toByte) |
|
65 |
||
66 |
"ca".toByte |
|
67 |
||
68 |
||
145
920f675b4ed1
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
69 |
// DFAs |
920f675b4ed1
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
70 |
import scala.util._ |
920f675b4ed1
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
71 |
|
920f675b4ed1
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
72 |
abstract class State |
920f675b4ed1
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
73 |
type States = Set[State] |
920f675b4ed1
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
74 |
|
920f675b4ed1
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
75 |
case class IntState(i: Int) extends State |
920f675b4ed1
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
76 |
|
920f675b4ed1
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
77 |
object NewState { |
920f675b4ed1
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
78 |
var counter = 0 |
920f675b4ed1
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
79 |
|
920f675b4ed1
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
80 |
def apply() : IntState = { |
920f675b4ed1
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
81 |
counter += 1; |
920f675b4ed1
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
82 |
new IntState(counter - 1) |
920f675b4ed1
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
83 |
} |
920f675b4ed1
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
84 |
} |
920f675b4ed1
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
85 |
|
920f675b4ed1
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
86 |
|
920f675b4ed1
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
87 |
// DFA class |
920f675b4ed1
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
88 |
case class DFA(states: States, |
920f675b4ed1
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
89 |
start: State, |
920f675b4ed1
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
90 |
delta: (State, Char) => State, |
920f675b4ed1
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
91 |
fins: States) { |
920f675b4ed1
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
92 |
|
920f675b4ed1
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
93 |
def deltas(q: State, s: List[Char]) : State = s match { |
920f675b4ed1
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
94 |
case Nil => q |
920f675b4ed1
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
95 |
case c::cs => deltas(delta(q, c), cs) |
920f675b4ed1
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
96 |
} |
920f675b4ed1
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
97 |
|
920f675b4ed1
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
98 |
// wether a string is accepted by the automaton or not |
920f675b4ed1
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
99 |
def accepts(s: String) : Boolean = |
920f675b4ed1
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
100 |
Try(fins contains |
920f675b4ed1
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
101 |
(deltas(start, s.toList))) getOrElse false |
920f675b4ed1
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
102 |
} |
920f675b4ed1
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
103 |
|
920f675b4ed1
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
104 |
|
920f675b4ed1
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
105 |
// example DFA from the lectures |
920f675b4ed1
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
106 |
val Q0 = NewState() |
920f675b4ed1
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
107 |
val Q1 = NewState() |
920f675b4ed1
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
108 |
val Q2 = NewState() |
920f675b4ed1
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
109 |
val Q3 = NewState() |
920f675b4ed1
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
110 |
val Q4 = NewState() |
920f675b4ed1
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
111 |
|
920f675b4ed1
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
112 |
|
920f675b4ed1
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
113 |
val delta : (State, Char) => State = { |
920f675b4ed1
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
114 |
case (Q0, 'a') => Q1 |
920f675b4ed1
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
115 |
case (Q0, 'b') => Q2 |
920f675b4ed1
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
116 |
case (Q1, 'a') => Q4 |
920f675b4ed1
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
117 |
case (Q1, 'b') => Q2 |
920f675b4ed1
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
118 |
case (Q2, 'a') => Q3 |
920f675b4ed1
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
119 |
case (Q2, 'b') => Q2 |
920f675b4ed1
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
120 |
case (Q3, 'a') => Q4 |
920f675b4ed1
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
121 |
case (Q3, 'b') => Q0 |
920f675b4ed1
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
122 |
case (Q4, 'a') => Q4 |
920f675b4ed1
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
123 |
case (Q4, 'b') => Q4 |
920f675b4ed1
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
124 |
} |
920f675b4ed1
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
125 |
|
920f675b4ed1
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
126 |
val DFA1 = DFA(Set(Q0, Q1, Q2, Q3, Q4), Q0, delta, Set(Q4)) |
920f675b4ed1
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
127 |
|
920f675b4ed1
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
128 |
println(DFA1.accepts("aaa")) |
920f675b4ed1
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
129 |
println(DFA1.accepts("bb")) |
920f675b4ed1
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
130 |
println(DFA1.accepts("aaac")) |
920f675b4ed1
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
131 |
|
920f675b4ed1
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
132 |