LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-02-2014, 05:48 AM   #1
kikilinux
Member
 
Registered: Sep 2012
Posts: 125

Rep: Reputation: Disabled
Question converting ip address to decimal value


I need to know the bellow code is correct to convert a IP address to equivalent decimal value ?
char ipChar[16];
struct in_addr ip;
printf("Read the IP Address to be converted\n");
scanf("%s",ipChar);
inet_aton(ipChar, &ip);
printf("ip address : %u\n", ip.s_addr);
If it is correct then the followed link http://electrofriends.com/source-cod...-bit-long-int/ has used the unsigned long int to convert ip to decimal?
at the end can I save the ip.s_addr to a uint32_t variables for my own use?
 
Old 10-02-2014, 06:08 AM   #2
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: Fedora
Posts: 4,140

Rep: Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263
No. That converts a string in decimal form to the binary form used by computers. If you look at the definition of the struct you will see that ipv4 is just a uint32_t so it is better to just keep the struct.

http://man7.org/linux/man-pages/man7/ip.7.html
 
  


Reply

Tags
conversion



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash scripting: converting integer to decimal Hawkallica Programming 7 10-01-2011 04:16 AM
Script for converting Hexadecimal IP to Decimal IP PKrishna Linux - Newbie 5 07-04-2008 07:49 PM
C++, issues converting decimal to fraction alpha_gamma Programming 7 06-04-2007 07:15 PM
Have problem converting a decimal number to octal Linh Programming 4 05-20-2004 03:21 PM
converting fake hex to decimal in c acid_kewpie Programming 10 08-20-2003 02:29 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:05 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration