Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

converting

Posted on 2012-04-06
2
Medium Priority
?
400 Views
Last Modified: 2012-08-14
Hello

Can i be given an example of how to convert a string to decimal.

ie.

char ip;

ip = 10.10.10.10

which should give me the decimal equivilant after conversion 168430090

thank you
0
Comment
Question by:codey-06
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 32

Assisted Solution

by:phoffric
phoffric earned 1000 total points
ID: 37817280
168430090 = 0x0A0A0A0A
breaking that down into bytes: 0x0A 0x0A 0x0A 0x0A ~ 10 10 10 10
So, you need to parse out the 4 decimal values.
One way is to use strtok to do this.
      http://www.cplusplus.com/reference/clibrary/cstring/strtok/
and then use atoi to convert to decimal
      http://www.cplusplus.com/reference/clibrary/cstdlib/atoi/
Now that you have the 4 integer values, you need to start shifting (by 8 bits at a time, using << operator) and cumulative adding on each value to come up with the 32-bit integer. This value can then be displayed either as unsigned decimal or hex.
0
 
LVL 86

Accepted Solution

by:
jkr earned 1000 total points
ID: 37817650
In that very case, 'inet_addr()' (http://pubs.opengroup.org/onlinepubs/007908799/xns/inet_addr.html) is your friend. I.e.

char* ip;
unsigned long addr;

ip = "10.10.10.10";

addr = inet_addr(ip);

Open in new window

0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The viewer will learn how to implement Singleton Design Pattern in Java.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question