If you mean flipping low with high half-bits of a Byte
:
(b: Byte) => (b >>> 4) + (b << 4)
If you want to flip every half-byte on a hex string
I would grouped
by 2
characters then flip
the pairs before creating the string
back again:
val input = "44[5e405f...".replaceAll("[^0-9a-f]","")
val expected = "44[e504f5...".replaceAll("[^0-9a-f]","")
// as suggested by @PaulDraper in the comments
input.grouped(2).map(_.reverse).mkString == expected
// or the verbose
val ab = "(.)(.)".r
input.grouped(2).map { case ab(a, b) => b ++ a }.mkString == expected
If your input is a List[Byte]
:
val inputBytes = input.grouped(2).map(Integer.parseInt(_, 16).toByte)
val expectedBytes = expected.grouped(2).map(Integer.parseInt(_, 16).toByte)
inputBytes.map((b: Byte) => (b >>> 4) + (b << 4)).map(_.toByte).toSeq == expectedBytes.toSeq