Reputation: 3
What is the best way to convert three bytes to an unsigned integer, from a binary file?
This is my current solution, do you know a better one?
a, b, c = file.read(3).unpack("C*")
a << 16 | b << 8 | c
Upvotes: 0
Views: 472
Reputation: 434585
You could do it all with unpack
if you didn't mind added an extra byte yourself:
n = *("\x00" + file.read(3)).unpack('N')
I don't know if that qualifies as better, that's pretty subjective.
Upvotes: 0