char * data = 0xFF000010FFFFFFFFFFFFFFFFFFFFFFFFFFF;
I want to get DOUBLE WORD in data[1] (0x00000010) and store it in var int i.
Would this do the trick?
int i = (int) data[1]+data[2]+data[3]+data[4]
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You are attempting to just add four bytes rather than position the values into the correct part of the integer. Without specifying the endianness of your platform, it’s not possible to provide a final answer.
The general approach is to place each byte in the correct position of the int, something like this:
int i = 256 * 256 * 256 * data[0] + 256 * 256 * data[1] + 256 * data[2] + data[3]
(big endian example)
Note that the indices are 0-based, not 1-based as in your example. The “base” in this example is 256 because each byte can represent 256 values.
To understand why this is so, consider the decimal number
5234
You can re-write that as:
5000 + 200 + 30 + 4
or 10 * 10 * 10 * 5 + 10 * 10 * 2 + 10 * 3 + 4
As you process data for each digit, you multiply the value by the-number-base-to-the-power-of-the-digit-position (rightmost digit for base 10 is 10^0, then 10^1, 10^2, etc).