{"id":71,"date":"2012-09-15T09:41:37","date_gmt":"2012-09-15T09:41:37","guid":{"rendered":"http:\/\/blog.tmyymmt.net\/en\/?p=71"},"modified":"2012-09-15T09:41:45","modified_gmt":"2012-09-15T09:41:45","slug":"converting-hex-string-and-byte-array-by-scala-fixed","status":"publish","type":"post","link":"https:\/\/blog.tmyymmt.net\/en\/?p=71","title":{"rendered":"Converting hex string and byte array by Scala fixed"},"content":{"rendered":"<p>Converting hex string to byte array, and byte array to hex string by Scala. Fixed to remove string except hex.<br \/>\n<script src=\"https:\/\/gist.github.com\/3727124.js\"><\/script><noscript><pre><code class=\"language-scala scala\">object HexBytesUtil {\n\n  def hex2bytes(hex: String): Array[Byte] = {\n    hex.replaceAll(&quot;[^0-9A-Fa-f]&quot;, &quot;&quot;).sliding(2, 2).toArray.map(Integer.parseInt(_, 16).toByte)\n  }\n\n  def bytes2hex(bytes: Array[Byte], sep: Option[String] = None): String = {\n    sep match {\n      case None =&gt; bytes.map(&quot;%02x&quot;.format(_)).mkString\n      case _ =&gt; bytes.map(&quot;%02x&quot;.format(_)).mkString(sep.get)\n    }\n    \/\/ bytes.foreach(println)\n  }\n\n  def example {\n    val data = &quot;48 65 6C 6C 6F 20 57 6F 72 6C 64 21 21&quot;\n    val bytes = hex2bytes(data)\n    println(bytes2hex(bytes, Option(&quot; &quot;)))\n\n    val data2 = &quot;48-65-6C-6C-6F-20-57-6F-72-6C-64-21-21&quot;\n    val bytes2 = hex2bytes(data2)\n    println(bytes2hex(bytes2, Option(&quot;-&quot;)))\n\n    val data3 = &quot;48656C6C6F20576F726C642121&quot;\n    val bytes3 = hex2bytes(data3)\n    println(bytes2hex(bytes3))\n  }\n\n}\n<\/code><\/pre><\/noscript><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Converting hex string to byte array, and byte array to hex string by Scala. Fixed to remove string except hex.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-71","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p2SiSL-19","_links":{"self":[{"href":"https:\/\/blog.tmyymmt.net\/en\/index.php?rest_route=\/wp\/v2\/posts\/71","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.tmyymmt.net\/en\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.tmyymmt.net\/en\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.tmyymmt.net\/en\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.tmyymmt.net\/en\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=71"}],"version-history":[{"count":2,"href":"https:\/\/blog.tmyymmt.net\/en\/index.php?rest_route=\/wp\/v2\/posts\/71\/revisions"}],"predecessor-version":[{"id":73,"href":"https:\/\/blog.tmyymmt.net\/en\/index.php?rest_route=\/wp\/v2\/posts\/71\/revisions\/73"}],"wp:attachment":[{"href":"https:\/\/blog.tmyymmt.net\/en\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=71"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.tmyymmt.net\/en\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=71"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.tmyymmt.net\/en\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=71"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}