- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- convert a 4 bytes to four 1 byte variables in c
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-25-2001 07:06 AM
тАО01-25-2001 07:06 AM
#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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-25-2001 10:31 AM
тАО01-25-2001 10:31 AM
Re: convert a 4 bytes to four 1 byte variables in c
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);
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-26-2001 07:24 AM
тАО01-26-2001 07:24 AM
Re: convert a 4 bytes to four 1 byte variables in c
#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);
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-26-2001 07:36 AM
тАО01-26-2001 07:36 AM
Re: convert a 4 bytes to four 1 byte variables in c
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-26-2001 10:25 AM
тАО01-26-2001 10:25 AM
SolutionIP 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);
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-26-2001 10:52 AM
тАО01-26-2001 10:52 AM
Re: convert a 4 bytes to four 1 byte variables in c
Thanks.