Solved

converting

Posted on 2012-04-06
2
387 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

762 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now