You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
34 lines
682 B
34 lines
682 B
package com.xsn.explorer.gcs
|
|
|
|
class UnsignedByte(val byte: Byte) extends AnyVal {
|
|
|
|
override def toString: String = {
|
|
toInt.toString
|
|
}
|
|
|
|
def toFixedBinaryString: String = {
|
|
val string = toInt.toBinaryString
|
|
val missing = List.fill(8 - string.length)(0).mkString("")
|
|
missing + string
|
|
}
|
|
|
|
def toInt: Int = byte.toInt & 0xFF
|
|
|
|
def bits: List[Bit] = {
|
|
toFixedBinaryString
|
|
.flatMap(Bit.from)
|
|
.toList
|
|
}
|
|
}
|
|
|
|
object UnsignedByte {
|
|
def parse(bits: List[Bit]): UnsignedByte = {
|
|
require(bits.size <= 8)
|
|
|
|
val int = bits.foldLeft(0) { case (acc, cur) =>
|
|
(acc * 2) + cur.toInt
|
|
}
|
|
|
|
new UnsignedByte(int.asInstanceOf[Byte])
|
|
}
|
|
}
|
|
|