Operating System - HP-UX
1821188 Members
3176 Online
109631 Solutions
New Discussion юеВ

Re: convert a 4 bytes to four 1 byte variables in c

 
SOLVED
Go to solution
Xiaoming Zhang
Contributor

convert a 4 bytes to four 1 byte variables in c

I ran following c program:

#include
main()
{
FILE *ftmp;
int index;
unsigned char aryIp[4];
unsigned long int lgIp;

lgIp =2559841793;

ftmp=fopen("ipaddr","w+b");
fwrite(&lgIp,4,1,ftmp);
fclose(ftmp);

ftmp=fopen("ipaddr","r+b");
for(index=0; index < 4; index++)
printf("%d\n",fgetc(ftmp));
}

and got the output as I expected:
152
148
22
1

Is there any simple way to split a 4 bytes long int variable to four one byte char?

Thanks

Xiaoming


5 REPLIES 5
Francisco Gazapo
Occasional Contributor

Re: convert a 4 bytes to four 1 byte variables in c

Hi,

look at this...


#include
main()
{
FILE *ftmp;
int index;
unsigned char *b1;
unsigned long int lgIp;

lgIp=2559841793;
b1=(char *)&lgIp;
printf ("%d,%d,%d,%d",*(b1+3),*(b1+2),*(b1+1),*b1);
}
Felix J. Liu
Advisor

Re: convert a 4 bytes to four 1 byte variables in c

How about this:

#include

main() {
unsigned long lgIp;
unsigned long byte=255;

lgIp = 2559841793;

printf("%d\n", (lgIp>>24)&byte);
printf("%d\n", (lgIp>>16)&byte);
printf("%d\n", (lgIp>>8)&byte);
printf("%d\n", lgIp&byte);
}
Xiaoming Zhang
Contributor

Re: convert a 4 bytes to four 1 byte variables in c

Your codes worked and my code became#include
main()
{
unsigned char *b1, aryChar[4], index;
unsigned long int lgIp;

lgIp=2559841793;
b1=(short int *)&lgIp;
for (index =0; index < 4; index++)
{
aryChar[index]=*(b1+index);
printf("%d.", aryChar[index]);
}
}

which actually produces a IP address from the unsigned long int.

Thanks.

Xiaoming
Gregory Fruth
Esteemed Contributor
Solution

Re: convert a 4 bytes to four 1 byte variables in c

You should be using inet_ntoa() and friends to work with
IP addresses. Any other method may not be portable (due to
byte ordering) and will break totally when the format of IP
addresses changes (IP4 -> IP6).

Even code using inet_ntoa() may have byte ordering problems
(depending on where the IP address came from) and problems
converting to IP6, but the problems will be smaller than those you'll
have if you do the conversion manually.

If you *must* do the conversion manually, a union is a better way
do it. The following code does the conversion using both
inet_ntoa() and a union.

(For a good discussion of how to work with IP addresses, refer
to the book "Unix Network Programming" by W. Richard Stevens.)

Good luck!

#include
#include
#include
#include

int main() {
unsigned long int lgIp;
struct in_addr addr;
char *aryIp;

union {
unsigned long int ulval;
unsigned char cval[4];
} uIp;

lgIp = 2559841793L;

/* convert using inet_ntoa() */
addr.s_addr = lgIp;
aryIp = inet_ntoa(addr);
printf("by inet_ntoa(): %s\n", aryIp);

/* note that the string returned by inet_ntoa() is in a static buffer,
so that the next call to inet_ntoa() will overwrite its contents.
therefore, you must copy it somewhere (via strdup() or
somesuch) if you want to use it later */

/* convert using union */
uIp.ulval = lgIp;
printf(" by union: %d.%d.%d.%d\n",
uIp.cval[0], uIp.cval[1], uIp.cval[2], uIp.cval[3]);

return(0);
}



Xiaoming Zhang
Contributor

Re: convert a 4 bytes to four 1 byte variables in c

The conversion by union is great!.

Thanks.