ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
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.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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:
If you contrive your array order correctly, you may be able to do it with a union, but this would be dependent upon byte order for the particular architecture.
Code:
union arrayToInt {
char array[4];
int value;
} convertor;
convertor.array[0] = 39;
convertor.array[1] = 30;
result = 0.5 * convertor.value;
The above will give you the something like "49403039". However, as suggested by theNbomr, the value of "value" depends upon byte order. You can AND (&) and shift (right/left) to meet your requirement.
hi carcassonne,
please note that you will have different results on big/little endian boxes.
If our are interessted in obtaining the mantisse/exponent part of the fp-number you may
prefer: frexp()
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 ;
OR
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.