progs/dfa.scala
changeset 479 52aa298211f6
parent 145 920f675b4ed1
child 481 acd8780bfc8b
--- a/progs/dfa.scala	Wed Mar 15 14:34:10 2017 +0000
+++ b/progs/dfa.scala	Fri Mar 17 12:15:58 2017 +0000
@@ -1,3 +1,71 @@
+// convert normal string to hex bytes string
+def string2hex(str: String): String = {
+  str.toList.map(_.toInt.toHexString).mkString
+}
+    
+// convert hex bytes string to normal string
+
+
+val byteArray: Array[Byte] = List.range(1, 255).map(_.toByte).toArray
+val bos = new BufferedOutputStream(new FileOutputStream("test.txt"))
+bos.write(byteArray)
+bos.close() 
+
+
+def string2int(hex: String) = {
+  hex.sliding(2, 2).map(Integer.parseInt(_, 16)).toArray
+}
+
+def test(l: List[Int]) = {
+  l.map(_.toChar).mkString
+}
+
+
+import java.io._
+val writer = new PrintWriter(new File("test.txt" ))
+
+//writer.write(string2int("cafebabe"))
+writer.write(test(List.range(1, 255)))
+writer.close()
+
+import scalax.io._
+import scalax.io.Resource
+
+FileOutputStream fos = new FileOutputStream("test");
+fos.write(bytesArray);
+fos.close();
+
+
+string2int("cafebabe")
+
+202.toHexString
+
+"cafebabe".sliding(1, 1).toList.map(Integer.parseInt(_, 16)).map(_.toByte).map(_.toChar)
+
+hex2string("ca")
+string2hex("ca")
+hex2string("cafebabe")
+
+    
+val appkey = "9GLV//lv/kYFW2o3/bihxwnMcmo="
+        
+        // string to hex
+        val appkey_hex = string2hex(appkey)
+        // 39474c562f2f6c762f6b594657326f332f62696878776e4d636d6f3d
+        println(appkey_hex)
+        
+        // hex to string
+        val appkey_string_again = hex2string(appkey_hex)
+        // 9GLV//lv/kYFW2o3/bihxwnMcmo=
+        println(appkey_string_again)
+    }
+
+
+List("ca", "fe", "ba", "be").map(_.toByte)
+
+"ca".toByte
+
+
 // DFAs
 import scala.util._