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.
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.
Introduction to Linux - A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free.
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.