preserves/_includes/cheatsheet-binary.md

37 lines
1.8 KiB
Markdown
Raw Normal View History

2023-03-15 15:24:02 +00:00
[varint]: https://developers.google.com/protocol-buffers/docs/encoding#varints
«#f» = [0x80]
«#t» = [0x81]
«#!V» = [0x86] ++ «V»
«V» if V ∈ Float = [0x87] ++ varint(|binary32(V)|) ++ binary32(V)
«V» if V ∈ Double = [0x87] ++ varint(|binary64(V)|) ++ binary64(V)
«V» if V ∈ SignedInteger = [0xB0] ++ varint(|intbytes(x)|) ++ intbytes(x)
«V» if V ∈ String = [0xB1] ++ varint(|utf8(V)|) ++ utf8(V)
«V» if V ∈ ByteString = [0xB2] ++ varint(|V|) ++ V
«V» if V ∈ Symbol = [0xB3] ++ varint(|utf8(V)|) ++ utf8(V)
«<L F_1...F_m>» = [0xB4] ++ «L» ++ «F_1» ++...++ «F_m» ++ [0x84]
«[X_1...X_m]» = [0xB5] ++ «X_1» ++...++ «X_m» ++ [0x84]
«#{E_1...E_m}» = [0xB6] ++ «E_1» ++...++ «E_m» ++ [0x84]
«{K_1:V_1...K_m:V_m}» = [0xB7] ++ «K_1» ++ «V_1» ++...++ «K_m» ++ «V_m» ++ [0x84]
«@V_1...@V_n V» = [0xBF] ++ «V» ++ «V_1» ++...++ «V_n» ++ [0x84]
Where
- `varint(m)` is the [varint-encoding][varint] of `m`; for example, `varint(15)` is `[0x0F]`,
and `varint(1000000000)` is `[0x80, 0x94, 0xeb, 0xdc, 0x03]`.
- `intbytes(x)` gives the big-endian two's-complement binary representation of `x`, taking
exactly as many whole bytes as needed to unambiguously identify the value and its sign. For
example, `intbytes(-128)` is `[0x80]`, `intbytes(-1)` is `[0xFF]`, `intbytes(0)` is `[]`,
`intbytes(1)` is `[0x01]`, `intbytes(128)` is `[0x00, 0x80]` etc.
- `utf8(S)` gives the sequence of bytes forming the UTF-8 encoding of the string `S`.
- `binary32(F)` and `binary64(D)` yield big-endian 4- and 8-byte IEEE 754 binary
representations of `F` and `D`, respectively.