如何使用Scala将String转换为ASCII

时间:2016-09-13 05:13:18

标签: scala ascii

我想将每个转换为十进制 ascii。
例如“RESEP”:
R = 82,
E = 69,
S = 83,
E = 69,
P = 80

我的代码是:

val LIST_KEYWORD = List("RESEP",
"DAGING SAPI",
"DAGING KAMBING")

val RUBAH_BYTE = LIST_KEYWORD.map(_.split(",")).map
{
  baris =>
    (
      baris(0).getBytes
    )
}

然后,我陷入困境,我不知道接下来该做什么。

1 个答案:

答案 0 :(得分:3)

scala> "RESEP".map(_.toByte)
res1: scala.collection.immutable.IndexedSeq[Byte] = Vector(82, 69, 83, 69, 80)

scala> "RESEP".map(x => x -> x.toByte)
res2: scala.collection.immutable.IndexedSeq[(Char, Byte)] = Vector((R,82), (E,69), (S,83), (E,69), (P,80))

scala> val LIST_KEYWORD = List("RESEP",
     | "DAGING SAPI",
     | "DAGING KAMBING")
LIST_KEYWORD: List[String] = List(RESEP, DAGING SAPI, DAGING KAMBING)

scala> LIST_KEYWORD.map(_.map(_.toByte))
res3: List[scala.collection.immutable.IndexedSeq[Byte]] = List(Vector(82, 69, 83, 69, 80), Vector(68, 65, 71, 73, 78, 71, 32, 83, 65, 80, 73), Vector(68, 65, 71, 73, 78, 71, 32, 75, 65, 77, 66, 73, 78, 71))

scala> LIST_KEYWORD.map(_.map(x => x -> x.toByte))
res4: List[scala.collection.immutable.IndexedSeq[(Char, Byte)]] = List(Vector((R,82), (E,69), (S,83), (E,69), (P,80)), Vector((D,68), (A,65), (G,71), (I,73), (N,78), (G,71), ( ,32), (S,83), (A,65), (P,80), (I,73)), Vector((D,68), (A,65), (G,71), (I,73), (N,78), (G,71), ( ,32), (K,75), (A,65), (M,77), (B,66), (I,73), (N,78), (G,71)))