Solved

converting

Posted on 2012-04-06
2
391 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
2 Comments
 
LVL 32

Assisted Solution

by:phoffric
phoffric earned 250 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 250 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
EvenOdd challenge 10 108
static method overriding vs normal non static method overriding in child class 13 97
Problem to open Excel file 15 135
Capture logon name 13 73
Navigation is an important part of web design from a usability perspective. But it is often a pain when it comes to a developer’s perspective. By navigation, it often means menuing. This is less theory and more practical of how to get a specific gro…
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

803 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