com.scalacraft.domain.v2.binary
An Octet represents an integer in the range [0, 255].
Octet
The following constraints hold for instances of this class,
- octet is in the range [0, 255]
octet
An instance can be created using a suitable overload of the opt method.
opt
val octetOpt1: Option[Octet] = Octet.opt(117) val octetOpt2: Option[Octet] = Octet.opt(0x75) val octetOpt3: Option[Octet] = Octet.opt("fe") // Decimal 254 val octetOpt4: Option[Octet] = Octet.opt("d") // Decimal 13 val octetOpt5: Option[Octet] = Octet.opt("12") // Decimal 18
Note that the string variant takes either one or two hex characters.
When any class constraint is violated the result is None.
None
val octetOpt: Option[Octet] = Octet.opt("NaN") // None
Pattern matching is supported as the following examples demonstrate,
7 match { case Octet(n) => n // 7 case _ => None }
The match target can be a string,
val s: String = "20" s match { case Octet(n) => n // 32 case _ => None }
Invalid octets are not matched,
-129 match { case Octet(n) => n case _ => None // None }
Implicit conversions exists which allow an instance of Octet to be used when an Int or String is required.
Int
String
val Some(hi) = Octet.opt(4) val Some(lo) = Octet.opt(3) val w: Int = 256 * hi + lo // 0x0403
A conversion to the unconstrained version of this class is also available.
A valid octet
An
Octet
represents an integer in the range [0, 255].The following constraints hold for instances of this class,
-
octet
is in the range [0, 255]An instance can be created using a suitable overload of the
opt
method.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
Octet
to be used when anInt
orString
is required.A conversion to the unconstrained version of this class is also available.