Created
June 9, 2017 10:43
-
-
Save adridadou/1a129a2139dcafb7127e87c4a7e62b67 to your computer and use it in GitHub Desktop.
Hex en/de coding
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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