Bytes.Order
type Bytes.Order = Bytes.BigEndian | Bytes.LittleEndianBytes.Order is the byte order of a multi-byte number in Bytes.read, Bytes.write and Bytes.append.
Details
- The two members are records with no fields, so a value is written with braces:
Bytes.BigEndian{},Bytes.LittleEndian{}. - A one-byte type,
byte,uint8orint8, takes an order and ignores it.
Basic examples
The same uint32 in each order. Bytes.BigEndian puts the most significant byte first, Bytes.LittleEndian the least significant:
let big = Bytes.new()
big.append(type(uint32), uint32(0x0a0b0c0d), Bytes.BigEndian{})
big // Bytes(0a 0b 0c 0d)
let little = Bytes.new()
little.append(type(uint32), uint32(0x0a0b0c0d), Bytes.LittleEndian{})
little // Bytes(0d 0c 0b 0a)The order decides which number the same bytes hold:
let b = Bytes.fromArray([0x01, 0x02])
b.read(type(uint16), 0, Bytes.BigEndian{}) // 258
b.read(type(uint16), 0, Bytes.LittleEndian{}) // 513Take Bytes.Order as a parameter to write code that works for either order:
func encodeAll(xs: Array[uint16], order: Bytes.Order): Bytes {
let out = Bytes.new()
for x in xs {
out.append(type(uint16), x, order)
}
out
}
encodeAll([uint16(1), uint16(2)], Bytes.BigEndian{}) // Bytes(00 01 00 02)
encodeAll([uint16(1), uint16(2)], Bytes.LittleEndian{}) // Bytes(01 00 02 00)Scope
A value of the union renders with its member's name, and compares equal to another of the same member:
let order: Bytes.Order = Bytes.BigEndian{}
order // Bytes.BigEndian{}
order is Bytes.BigEndian // true
order == Bytes.LittleEndian{} // false
Bytes.LittleEndian{} == Bytes.LittleEndian{} // true
String.from(Bytes.LittleEndian{}) // "Bytes.LittleEndian{}"Keep the order in a name and pass it to every call of one format:
let order = Bytes.LittleEndian{}
let b = Bytes.new()
b.append(type(uint16), uint16(1), order)
b.append(type(int32), int32(-1), order)
b // Bytes(01 00 ff ff ff ff)
b.read(type(int32), 2, order) // -1Choose an order with an if expression:
let littleEndian = true
let order: Bytes.Order = if littleEndian { Bytes.LittleEndian{} } else { Bytes.BigEndian{} }
order // Bytes.LittleEndian{}Test the member with is to branch on it:
func describe(order: Bytes.Order): string {
if order is Bytes.BigEndian {
"most significant byte first"
} else {
"least significant byte first"
}
}
describe(Bytes.BigEndian{}) // "most significant byte first"
describe(Bytes.LittleEndian{}) // "least significant byte first"A record field can hold an order:
type Format = { name: string, order: Bytes.Order }
let formats = [
Format{ name: "network", order: Bytes.BigEndian{} },
Format{ name: "x86", order: Bytes.LittleEndian{} },
]
let big = formats | Array.filter({ x.order is Bytes.BigEndian }) | Array.map({ x.name })
big // ["network"]Applications
A TIFF file states its own byte order in its first two bytes, II for little-endian and MM for big-endian. Read the order from the header, then use it for every number after it. A header shorter than four bytes fails before the read:
func byteOrder(header: Bytes): Result[Bytes.Order, Error] {
if header.startsWith("II".toBytes()) {
return Result.ok(Bytes.LittleEndian{})
}
if header.startsWith("MM".toBytes()) {
return Result.ok(Bytes.BigEndian{})
}
Result.fail("not a TIFF file")
}
func magic(header: Bytes): Result[uint16, Error] {
if header.length() < 4 {
return Result.fail("not a TIFF file")
}
let order = try byteOrder(header)
Result.ok(header.read(type(uint16), 2, order))
}
println(magic(Bytes.fromArray([0x49, 0x49, 0x2a, 0x00])))
println(magic(Bytes.fromArray([0x4d, 0x4d, 0x00, 0x2a])))
println(magic(Bytes.fromArray([0x50, 0x4b, 0x03, 0x04])))
println(magic(Bytes.fromArray([0x49, 0x49])))Ok{ value: 42 }
Ok{ value: 42 }
Err{ error: Error{ message: "not a TIFF file" } }
Err{ error: Error{ message: "not a TIFF file" } }Convert a number from one order to the other by writing it in one and reading it in the other:
func swap32(x: uint32): uint32 {
let b = Bytes.new()
b.append(type(uint32), x, Bytes.BigEndian{})
b.read(type(uint32), 0, Bytes.LittleEndian{})
}
swap32(uint32(1)) // 16777216
swap32(uint32(0x11223344)) == uint32(0x44332211) // trueProperties and relations
The bytes of one order are the bytes of the other reversed:
let big = Bytes.new()
let little = Bytes.new()
big.append(type(float64), 1.5, Bytes.BigEndian{})
little.append(type(float64), 1.5, Bytes.LittleEndian{})
assert(big.toArray().reverse() == little.toArray())Reading in the order that wrote gives the number back:
let orders: Array[Bytes.Order] = [Bytes.BigEndian{}, Bytes.LittleEndian{}]
for order in orders {
let b = Bytes.new()
b.append(type(int64), -123456789, order)
assert(b.read(type(int64), 0, order) == -123456789)
}A one-byte type gives the same byte in either order:
let big = Bytes.new()
let little = Bytes.new()
big.append(type(int8), int8(-2), Bytes.BigEndian{})
little.append(type(int8), int8(-2), Bytes.LittleEndian{})
assert(big == little)Possible issues
Reading in the order the data was not written in gives a different number, with no error:
let b = Bytes.new()
b.append(type(uint32), uint32(1), Bytes.LittleEndian{})
b.read(type(uint32), 0, Bytes.BigEndian{}) // 16777216The order is a value, so it takes {}:
let b = Bytes.fromArray([1, 2])
println(b.read(type(uint16), 0, Bytes.BigEndian))
// error: Bytes.BigEndian is a type, not a value; a type appears only as a literal head, after is, or before a dotThere is no default order. Every call states one:
let b = Bytes.fromArray([1, 2])
println(b.read(type(uint16), 0)) // error: Bytes.read needs an argument for orderSee also
Bytes.BigEndian · Bytes.LittleEndian · Bytes.read · Bytes.write · Bytes.append · Bytes.sizeOf · Bytes
