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.
I need to convert to a long inetger value to byte array in c so that i can access the four byte value of
long integer separately.Could anyone please suggest me a way of doing this.
I would like to elaborate the problem,
long int testval = 3840 ;
The binary value of the above long value (3840)is
0000 0000 // first byte
0000 0000 // second byte
0000 1111 //third byte
0000 0000 // fourth byte
What i need is a way to get the byte values separately and store in char [4] in c, so that
char[0] = 0000 0000 // first byte
char [1] = 0000 0000 // second byte
char [2] = 0000 1111 //third byte
char [3] = 0000 0000 //fourth byte
Could someone help me to solve this.
Thanks for your time,
#include <stdio.h>
main(){
unsigned long int x = 4194967296;
unsigned int a = x % 256;
unsigned int b = x % 65536 - a;
unsigned int c = x % 16777216 - (a + b) ;
unsigned int d = x % 4294967296 - (a + b + c);
printf("First byte:\t%u\n",a);
printf("Second byte:\t%u\n",b);
printf("Third byte:\t%u\n",c);
printf("Fourth byte:\t%u\n",d);
}
You must use the % operator in order to 'select' a determined length of bits.
This is called masking. Say you've got this binary number 10011100. You want to cut it up into 4 pieces. You can use the masks 00000011, 00001100, 00110000, and 11000000. If you did 1001100 & 00110000 you'd get 00010000, but then you have to shift the bits to the right 4 times to get 00000001.
Note that on a little endian machine the last thing printed above would be the most significant byte, but on a big endian machine, the last thing printed would be the least significant byte.
Using itsme86's method, you avoid having to know about the endianness because you are not directly accessing the way the integer is stored. (3840 & 0xFF00) >> 8 is going to be the same no matter if it's stored as big or little endian...
Nope, you're right. You didn't claim yours was dependent on the endianness, though. Also, generally when doing this type of stuff, I tend to think of it in terms of bitmasks the way itsme86 showed rather than in terms of using the modulus as you did in your example... It doesn't make your way any less relevent, though.
Actually, after looking at it closer, your method isn't quite right. You still need to do a bit shift on them. (Or to go more along with the spirit of using the modulus, the appropriate division...)
Just to recap, here's a program that uses all 3 methods... they all print the same thing on a little endian machine, but on a big endian machine the last method will have the bytes in the reverse order.
Code:
#include <stdio.h>
main()
{
unsigned long int x = 4194967296;
unsigned int a = x % 256;
unsigned int b = (x % 65536 - a)/256;
unsigned int c = (x % 16777216 - (a + b))/65536;
unsigned int d = (x % 4294967296 - (a + b + c))/16777216;
unsigned char* pX = &x;
printf("First byte:\t0x%.2x\n",a);
printf("Second byte:\t0x%.2x\n",b);
printf("Third byte:\t0x%.2x\n",c);
printf("Fourth byte:\t0x%.2x\n",d);
printf("=====================\n");
printf("First byte:\t0x%.2x\n", x & 0xFF);
printf("Second byte:\t0x%.2x\n", (x & 0xFF00) >> 8);
printf("Third byte:\t0x%.2x\n", (x & 0xFF0000) >> 16);
printf("Fourth byte:\t0x%.2x\n", (x & 0xFF000000) >> 24);
printf("=====================\n");
printf("First byte:\t0x%.2x\n", pX[0]);
printf("Second byte:\t0x%.2x\n", pX[1]);
printf("Third byte:\t0x%.2x\n", pX[2]);
printf("Fourth byte:\t0x%.2x\n", pX[3]);
}
Results:
Code:
First byte: 0x00
Second byte: 0x1f
Third byte: 0x0a
Fourth byte: 0xfa
=====================
First byte: 0x00
Second byte: 0x1f
Third byte: 0x0a
Fourth byte: 0xfa
=====================
First byte: 0x00
Second byte: 0x1f
Third byte: 0x0a
Fourth byte: 0xfa
I should also note that this is with a 32-bit compiler on an 32-bit architecture... a 64-bit compiler on a 64-bit architecture would likely define a long int as 64 bits instead of 32, so in that case, the method of casting to a char* or using a union as in the latest example would likely be grabbing the bytes from the wrong spot anyway....
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.