src/Pure/General/bytes.scala
changeset 68149 9a4a6adb95b5
parent 68108 2277fe496d78
child 68150 f0f34cbed539
equal deleted inserted replaced
68148:fb661e4c4717 68149:9a4a6adb95b5
    37       val b = new Array[Byte](length)
    37       val b = new Array[Byte](length)
    38       System.arraycopy(a, offset, b, 0, length)
    38       System.arraycopy(a, offset, b, 0, length)
    39       new Bytes(b, 0, b.length)
    39       new Bytes(b, 0, b.length)
    40     }
    40     }
    41 
    41 
    42 
       
    43   def hex(s: String): Bytes =
       
    44   {
       
    45     def err(): Nothing = error("Malformed hexadecimal representation of bytes\n" + s)
       
    46     val len = s.length
       
    47     if (len % 2 != 0) err()
       
    48 
       
    49     val n = len / 2
       
    50     val a = new Array[Byte](n)
       
    51     for (i <- 0 until n) {
       
    52       val j = 2 * i
       
    53       try { a(i) = Integer.parseInt(s.substring(j, j + 2), 16).toByte }
       
    54       catch { case _: NumberFormatException => err() }
       
    55     }
       
    56     new Bytes(a, 0, n)
       
    57   }
       
    58 
    42 
    59   def base64(s: String): Bytes =
    43   def base64(s: String): Bytes =
    60   {
    44   {
    61     val a = Base64.getDecoder.decode(s)
    45     val a = Base64.getDecoder.decode(s)
    62     new Bytes(a, 0, a.length)
    46     new Bytes(a, 0, a.length)