ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
How can I take, in C, the first two bytes and either put them in an integer to do the above math or directly them to perform the calculation ? I tried the following, typecasting the beginning of the array so that the compiler might think it's an integer, but it does not work:
You should not try to exploit the order of values as they are stored in the array, since this will be completely non-portable. Simply get the components of your desired integer value and merge them together:
int highbyte = 0x30;
int lowbyte = 0x39;
For int value, either method below works and is reliably portable (assuming only that bytes are 8-bits wide and an int is large enough to store two bytes)
value = ( highbyte * 256 ) + lowbyte ;
value = ( highbyte << 8 ) | lowbyte ;
Then you can work with the integer result of "value" however you like.
Sorry for the confusion. The example was for an array of 4 numeric chars and I copied the example array w/o ensuring that all of the chars were numeric. That said, if you are only interested in the first 2 chars then change the loop control test to i < 2 so only the first 2 characters are processed.