Skip to content

Instantly share code, notes, and snippets.

@adridadou
Created June 9, 2017 10:43
Show Gist options
  • Save adridadou/1a129a2139dcafb7127e87c4a7e62b67 to your computer and use it in GitHub Desktop.
Save adridadou/1a129a2139dcafb7127e87c4a7e62b67 to your computer and use it in GitHub Desktop.
Hex en/de coding
object Hex {
def hex2bytes(hex: String): Array[Byte] = {
hex.replaceAll("[^0-9A-Fa-f]", "").sliding(2, 2).toArray.map(Integer.parseInt(_, 16).toByte)
}
def bytes2hex(bytes: Array[Byte], sep: Option[String] = None): String = {
sep match {
case None => bytes.map("%02x".format(_)).mkString
case _ => bytes.map("%02x".format(_)).mkString(sep.get)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment