Solved

converting

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

730 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