{"id":67,"date":"2012-09-14T10:39:31","date_gmt":"2012-09-14T10:39:31","guid":{"rendered":"http:\/\/blog.tmyymmt.net\/en\/?p=67"},"modified":"2012-09-14T10:39:31","modified_gmt":"2012-09-14T10:39:31","slug":"converting-hex-string-and-byte-array-by-scala","status":"publish","type":"post","link":"https:\/\/blog.tmyymmt.net\/en\/?p=67","title":{"rendered":"Converting hex string and byte array by Scala"},"content":{"rendered":"<p>Converting hex string to byte array, and byte array to hex string by Scala.<\/p>\n<script src=\"https:\/\/gist.github.com\/3721117.js\"><\/script><noscript><pre><code class=\"language-scala scala\">object HexBytesUtil {\n\n  def hex2bytes(hex: String): Array[Byte] = {\n    if(hex.contains(&quot; &quot;)){\n      hex.split(&quot; &quot;).map(Integer.parseInt(_, 16).toByte)\n    } else if(hex.contains(&quot;-&quot;)){\n      hex.split(&quot;-&quot;).map(Integer.parseInt(_, 16).toByte)\n    } else {\n      hex.sliding(2,2).toArray.map(Integer.parseInt(_, 16).toByte)\n    }\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>\n","protected":false},"excerpt":{"rendered":"<p>Converting hex string to byte array, and byte array to hex string by Scala.<\/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-67","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p2SiSL-15","_links":{"self":[{"href":"https:\/\/blog.tmyymmt.net\/en\/index.php?rest_route=\/wp\/v2\/posts\/67","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=67"}],"version-history":[{"count":3,"href":"https:\/\/blog.tmyymmt.net\/en\/index.php?rest_route=\/wp\/v2\/posts\/67\/revisions"}],"predecessor-version":[{"id":70,"href":"https:\/\/blog.tmyymmt.net\/en\/index.php?rest_route=\/wp\/v2\/posts\/67\/revisions\/70"}],"wp:attachment":[{"href":"https:\/\/blog.tmyymmt.net\/en\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=67"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.tmyymmt.net\/en\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=67"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.tmyymmt.net\/en\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=67"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}