An Octet represents an integer in the range [0, 255].
An OctetPair represents two Octets.
An OctetPair represents two Octets.
The following constraints hold for instances of this class,
- hi is not null
- lo is not null
An instance can be created using a suitable overload of the opt method.
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.
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.
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.
An
Octetrepresents an integer in the range [0, 255].The following constraints hold for instances of this class,
-
octetis in the range [0, 255]An instance can be created using a suitable overload of the
optmethod.Note that the string variant takes either one or two 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 octets are not matched,
Implicit Conversions
Implicit conversions exists which allow an instance of
Octetto be used when anIntorStringis required.A conversion to the unconstrained version of this class is also available.