+ # Return self as a C hexadecimal digest where bytes are prefixed by `\x`
+ #
+ # The output is compatible with literal stream of bytes for most languages
+ # including C and Nit.
+ #
+ # ~~~
+ # var b = "abcd".to_bytes
+ # assert b.chexdigest == "\\x61\\x62\\x63\\x64"
+ # assert b.chexdigest.unescape_to_bytes == b
+ # ~~~
+ fun chexdigest: String do
+ var elen = length * 4
+ var ns = new CString(elen)
+ var i = 0
+ var oi = 0
+ while i < length do
+ ns[oi] = 0x5Cu8 # b'\\'
+ ns[oi+1] = 0x78u8 # b'x'
+ self[i].add_digest_at(ns, oi+2)
+ i += 1
+ oi += 4
+ end
+ return new FlatString.full(ns, elen, 0, elen)
+ end
+
+
+ # Returns self as a stream of bits (0 and 1)
+ #
+ # ~~~
+ # var b = "abcd".to_bytes
+ # assert b.binarydigest == "01100001011000100110001101100100"
+ # assert b.binarydigest.binarydigest_to_bytes == b
+ # ~~~
+ fun binarydigest: String do
+ var elen = length * 8
+ var ns = new CString(elen)
+ var i = 0
+ var oi = 0
+ while i < length do
+ var c = self[i]
+ var b = 128u8
+ while b > 0u8 do
+ if c & b == 0u8 then
+ ns[oi] = 0x30u8 # b'0'
+ else
+ ns[oi] = 0x31u8 # b'1'
+ end
+ oi += 1
+ b = b >> 1
+ end
+ i += 1
+ end
+ return new FlatString.full(ns, elen, 0, elen)
+ end
+
+ # Interprets `self` as a big-endian positive integer.
+ #
+ # ~~~
+ # var b = "0102".hexdigest_to_bytes
+ # assert b.to_i == 258
+ # ~~~
+ #
+ # Nul bytes on the left are trimmed.
+ # 0 is returned for an empty Bytes object.
+ #
+ # ~~~
+ # assert "01".hexdigest_to_bytes.to_i == 1
+ # assert "0001".hexdigest_to_bytes.to_i == 1
+ #
+ # assert "0000".hexdigest_to_bytes.to_i == 0
+ # assert "00".hexdigest_to_bytes.to_i == 0
+ # assert "".hexdigest_to_bytes.to_i == 0
+ # ~~~
+ #
+ # `Int::to_bytes` is a loosely reverse method.
+ #
+ # ~~~
+ # assert b.to_i.to_bytes == b
+ # assert (b.to_i + 1).to_bytes.hexdigest == "0103"
+ # assert "0001".hexdigest_to_bytes.to_i.to_bytes.hexdigest == "01"
+ # ~~~
+ #
+ # Warning: `Int` might overflow for bytes with more than 60 bits.
+ fun to_i: Int do
+ var res = 0
+ var i = 0
+ while i < length do
+ res *= 256
+ res += self[i].to_i
+ i += 1
+ end
+ return res
+ end
+