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.
Hi:
I have variable chk which is type unsigned long. Let's suppose this variable occupies four bytes (if it does not that's another problem). The program transmits a file over a line byte by byte at the same time maintaining a checksum which it transmit when done transmitting the file. The problem is I must transmit chk one byte at a time. How do I get access to the four bytes making chk? If I could declare
unsigned long chk;
unsigned char byte0 (at the same location as chk);
unsigned char byte1
unsigned char byte2
unsigned char byte3
then byte0 would be the least significant byte of chk. Also, I could do
lsbyte= chk mod 256 with the same result but I do not want to do arithmetic. Can two variables be declared so that they share the same location?
One is pointers: make 4 pointers, one for each byte.
byte0 = address of chk+(0 * sizeof char);
byte1 = address of chk+(1 * sizeof char);
byte2 = address of chk+(2 * sizeof char);
byte3 = address of chk+(3 * sizeof char);
I think that one is the most elegant;
On the other hand, you can also use the values by shifting around chk.
then byte0 would be the least significant byte of chk.
Actually, that depends on the system endianness. When sending data over network, you have to allways define your transmission protocol so that the code works regardless of the byte order of both machines.
Quote:
Originally Posted by stf92
Also, I could do
lsbyte= chk mod 256 with the same result but I do not want to do arithmetic.
You have to do arithmetic, be it an int arithmetic or pointer arithmetic.
Quote:
Originally Posted by stf92
Can two variables be declared so that they share the same location?
You can isolate bytes within a multibyte word with 'and's and 'shift's:
Code:
unsigned long word; // assume 4-byte word, per your spec.
unsigned int chksum;
word = 0x12345678;
chksum = word & 0x000000ff;
chksum += ( word & 0x0000ff00 ) >> 8;
chksum += ( word & 0x00ff0000 ) >> 16;
chksum += ( word & 0xff000000 ) >> 24;
The use of unions as a form of 'conversion' memory space will only work in this case if the order of the bytes in unimportant (such as the way I've computed a simple sum checksum in the prior article). In a more 'CRC-like' scenario, where shifts and X-ORs are incorporated, the algorithm is sensitive to the byte ordering. The C language does not specify byte order of multibyte words, so the code would be unportable.
Hi:
I have variable chk which is type unsigned long. Let's suppose this variable occupies four bytes (if it does not that's another problem). The program transmits a file over a line byte by byte at the same time maintaining a checksum which it transmit when done transmitting the file. The problem is I must transmit chk one byte at a time. How do I get access to the four bytes making chk? If I could declare
unsigned long chk;
unsigned char byte0 (at the same location as chk);
unsigned char byte1
unsigned char byte2
unsigned char byte3
then byte0 would be the least significant byte of chk. Also, I could do
lsbyte= chk mod 256 with the same result but I do not want to do arithmetic. Can two variables be declared so that they share the same location?
Quote:
I have variable chk which is type unsigned long. Let's suppose this variable occupies four bytes (if it does not that's another problem)
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.