c - Getting a double word from binary data -
c - Getting a double word from binary data -
char * info = 0xff000010fffffffffffffffffffffffffff;
i want double word in data[1] (0x00000010)
, store in var int i
.
would trick?
int = (int) data[1]+data[2]+data[3]+data[4]
you attempting add together 4 bytes rather position values right part of integer. without specifying endianness of platform, it's not possible provide final answer.
the general approach place each byte in right position of int, this:
int = 256 * 256 * 256 * data[0] + 256 * 256 * data[1] + 256 * data[2] + data[3]
(big endian example)
note indices 0-based, not 1-based in example. "base" in illustration 256 because each byte can represent 256 values.
to understand why so, consider decimal number
5234
you can re-write as:
5000 + 200 + 30 + 4
or 10 * 10 * 10 * 5 + 10 * 10 * 2 + 10 * 3 + 4
as process info each digit, multiply value the-number-base-to-the-power-of-the-digit-position (rightmost digit base of operations 10 10^0, 10^1, 10^2, etc).
c binary
Comments
Post a Comment