com.scalacraft.domain.v2.binary
An OctetPair represents two Octets.
OctetPair
The following constraints hold for instances of this class,
- hi is not null - lo is not null
hi
lo
An instance can be created using a suitable overload of the opt method.
opt
val op1: Option[OctetPair] = OctetPair.opt(34540) // OctetPair(134, 236) = 34540 val op2: Option[OctetPair] = OctetPair.opt(0x86ec) // OctetPair(134, 236) = 34540 val op3: Option[OctetPair] = OctetPair.opt("d") // OctetPair(0, 13) val op3: Option[OctetPair] = OctetPair.opt("fe") // OctetPair(0, 254) val op5: Option[OctetPair] = OctetPair.opt("012") // Decimal 18 val op4: Option[OctetPair] = OctetPair.opt("f11d") // Decimal 61725
Note that the string variant takes between one to four hex characters.
When any class constraint is violated the result is None.
None
val op1: Option[OctetPair] = OctetPair.opt("NaN") // None val op2: Option[OctetPair] = OctetPair.opt(0xf1234) // None
Pattern matching is supported as the following examples demonstrate,
0x3490 match { case OctetPair(hi, lo) => (hi, lo) // (Octet(0x34), Octet(0x90) case _ => None }
The match target can be a string,
val s: String = "4020" s match { case OctetPair(hi, lo) => (hi, lo) // (Octet(0x40), Octet(0x20) case _ => None }
Invalid octet pairs are not matched,
-129 match { case OctetPair(hi, lo) => (hi, lo) case _ => None // None }
Implicit conversions are supplied which allow an instance of OctetPair to be used when an Int or String is required.
Int
String
val Some(pair) = OctetPair.opt(0x40cc) val w: Int = 1 + pair // 0x40cd
val Some(pair) = OctetPair.opt("f") val s: String = pair: String // 000f
A conversion to the unconstrained version of this class is also available.
A valid octet representing the high byte
A valid octet representing the low byte
An
OctetPair
represents two Octets.The following constraints hold for instances of this class,
-
hi
is not null -lo
is not nullAn instance can be created using a suitable overload of the
opt
method.Note that the string variant takes between one to four hex characters.
When any class constraint is violated the result is
None
.Pattern Matching
Pattern matching is supported as the following examples demonstrate,
The match target can be a string,
Invalid octet pairs are not matched,
Implicit Conversions
Implicit conversions are supplied which allow an instance of
OctetPair
to be used when anInt
orString
is required.A conversion to the unconstrained version of this class is also available.