title: Decoding a String with escaped special characters in Scala issue
body: <p>I have a multi-line JSON file with records that contain special characters encoded as hexadecimals. Here is an example of a single JSON record:</p>
  
  <p><code>{\x22value\x22:\x22\xC4\xB1arines Bint\xC4\xB1\xC3\xA7 Ramu\xC3\xA7lar\x22}</code></p>
  
  <p>This record is supposed to be <code>{"value":"ıarines Bintıç Ramuçlar"}</code> , e.g. '"' character are replaced with corresponding hexadecimal \x22 and other special Unicode characters are replaced with one or two  hexadecimals (for instance \xC3\xA7 encodes ç, etc.)</p>
  
  <p>I need to convert similar Strings into a regular Unicode String in Scala, so when printed it produced <code>{"value":"ıarines Bintıç Ramuçlar"}</code> without hexadecimals.</p>
  
  <p>In Python I can easily decode these records with a line of code:</p>
  
  <pre><code>&gt;&gt;&gt; a = "{\x22value\x22:\x22\xC4\xB1arines Bint\xC4\xB1\xC3\xA7 Ramu\xC3\xA7lar\x22}"
  &gt;&gt;&gt; a.decode("utf-8")
  u'{"value":"\u0131arines Bint\u0131\xe7 Ramu\xe7lar"}'
  &gt;&gt;&gt; print a.decode("utf-8")
  {"value":"ıarines Bintıç Ramuçlar"}
  </code></pre>
  
  <p>But in Scala I can't find a way to decode it. I unsuccessfully tried to convert it like this:</p>
  
  <pre><code>scala&gt; val a = """{\x22value\x22:\x22\xC4\xB1arines Bint\xC4\xB1\xC3\xA7 Ramu\xC3\xA7lar\x22}"""
  scala&gt; print(new String(a.getBytes(), "UTF-8"))
  {\x22value\x22:\x22\xC4\xB1arines Bint\xC4\xB1\xC3\xA7 Ramu\xC3\xA7lar\x22}
  </code></pre>
  
  <p>I also tried URLDecoder as I found in solution for similar problem (but with URL):</p>
  
  <pre><code>scala&gt; val a = """{\x22value\x22:\x22\xC4\xB1arines Bint\xC4\xB1\xC3\xA7 Ramu\xC3\xA7lar\x22}"""
  scala&gt; print(java.net.URLDecoder.decode(a.replace("\\x", "%"), "UTF-8"))
  {"value":"ıarines Bintıç Ramuçlar"}
  </code></pre>
  
  <p>It produced the desired result for this example but is seems not safe for generic text fields since it designed to work with URLs and requires replacing all <code>\x</code> to <code>%</code> in the string.</p>
  
  <p><strong>Does Scala have some better way to deal with this issue?</strong> </p>
  
  <p>I am new to Scala and will be thankful for any help</p>
  
  <p><strong>UPDATE</strong>:
  I have made a custom solution with <code>javax.xml.bind.DatatypeConverter.parseHexBinary</code>. It works for now, but it seems cumbersome and not at all elegant. I think there should be a simpler way to do this.</p>
  
  <p>Here is the code:</p>
  
  <pre><code>import javax.xml.bind.DatatypeConverter
  import scala.annotation.tailrec
  import scala.util.matching.Regex
  
  def decodeHexChars(string: String): String = {
    val regexHex: Regex = """\A\\[xX]([0-9a-fA-F]{1,2})(.*)""".r
    def purgeBuffer(buffer: String, acc: List[Char]): List[Char] = {
      if (buffer.isEmpty) acc
      else new String(DatatypeConverter.parseHexBinary(buffer)).reverse.toList ::: acc
    }
    @tailrec
    def traverse(s: String, acc: List[Char], buffer: String): String = s match {
      case "" =&gt;
        val accUpdated = purgeBuffer(buffer, acc)
        accUpdated.foldRight("")((str, b) =&gt; b + str)
      case regexHex(chars, suffix) =&gt;
        traverse(suffix, acc, buffer + chars)
      case _ =&gt;
        val accUpdated = purgeBuffer(buffer, acc)
        traverse(s.tail, s.head :: accUpdated, "")
    }
    traverse(string, Nil, "")
  }
  </code></pre>