Richard Searle

home

Composing scodec-bit hex representation

16 Feb 2015

The scodec-bits project provides convenient mechanisms to specify constant data:

val x: ByteVector = hex"deadbeef"
val y: BitVector = bin"00101101010010101"

This syntax is quite clear until you need to assemble a ByteVector from pieces:

val prefixed: ByteVector = hex"0102$x"

Note that the referenced variable must be a ByteVector!

Placing the substitution within the string requires an expression, rather than the simple variable reference.

val wrapped: ByteVector = hex"0102${x}0304"

An alternative is to explicitly concatenate the ByteVectors:

val prefixed: ByteVector = hex"0102" ++ x