Welcome to Dream.In.Code
Become a C++ Expert!

Join 150,218 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,169 people online right now. Registration is fast and FREE... Join Now!




HEXA to DEC

 
Reply to this topicStart new topic

HEXA to DEC

RauLiTo
29 Nov, 2007 - 05:52 AM
Post #1

New D.I.C Head
*

Joined: 28 Nov, 2007
Posts: 19


My Contributions
hi guys ... i want to write a program to convert a hexadecimal number to a decimal number using c++ ... can anyone help me in writing that program?

help me please biggrin.gif icon_up.gif
User is offlineProfile CardPM
+Quote Post

Louisda16th
RE: HEXA To DEC
29 Nov, 2007 - 06:43 AM
Post #2

 101010101
Group Icon

Joined: 3 Aug, 2006
Posts: 1,812



Thanked: 1 times
Dream Kudos: 755
My Contributions
Please post your relevant code. If you do not know how the conversion is done, read that first. Google is your friend.

This post has been edited by Louisda16th: 29 Nov, 2007 - 06:44 AM
User is offlineProfile CardPM
+Quote Post

RauLiTo
RE: HEXA To DEC
29 Nov, 2007 - 06:54 AM
Post #3

New D.I.C Head
*

Joined: 28 Nov, 2007
Posts: 19


My Contributions
i want to write a program to convert binary , octan or hexadecimal number to decimal ... i wrote it if its binary or octal and it was easy because its just numbers but in hexadecimal i have to work with numbers and letters which mean i have to use string ... and the main problem with the string how can i deal with each digit if its a string ? its easy if its number to get each digit alone by using mod and division ... but if they are letters i cant divide or use the mod ...

here is my work :


#include<iostream>
#include<cmath>
#include<string>
using namespace std;
int main()

{
int base,num,numC=0,counterC=0;

cout<<"please enter the base of the number you want to convert : ";
cin>>base;
cout<<endl;

switch(base)
{

case 2:

cout<<"Enter a number in the binary base : ";
cin>>num;

while(num>0)
{

switch(num%10)
{

case 0: numC+=0;
case 1:numC+=pow(base,counterC);

}

num=num/10;
counterC++;
}

cout<<"The number in the decimal is : "<<numC<<endl<<endl;

break;

case 8:

cout<<"Enter a number in the octal base : ";
cin>>num;

while(num>0)
{

switch(num%10)
{

case 0:numC+=0;
case 1:numC+=pow(base,counterC);
case 2:numC+=2*pow(base,counterC);
case 3:numC+=3*pow(base,counterC);
case 4:numC+=4*pow(base,counterC);
case 5:numC+=5*pow(base,counterC);
case 6:numC+=6*pow(base,counterC);
case 7:numC+=7*pow(base,counterC);

}

num=num/10;
counterC++;

}

cout<<"The number in the decimal is : "<<numC<<endl<<endl;

break;

case 16:

cout<<"Enter a number in the hexadecimal base : ";
cin>>num;

switch(num)
{
case 0:numC+=0;
case 1:numC+=pow(base,counterC);
case 2:numC+=2*pow(base,counterC);
case 3:numC+=3*pow(base,counterC);
case 4:numC+=4*pow(base,counterC);
case 5:numC+=5*pow(base,counterC);
case 6:numC+=6*pow(base,counterC);
case 7:numC+=7*pow(base,counterC);
case 8:numC+=8*pow(base,counterC);
case 9:numC+=9*pow(base,counterC);
case 'A':case 'a':numC+=10*pow(base,counterC);
case 'B':case 'b':numC+=11*pow(base,counterC);
case 'C':case 'c':numC+=12*pow(base,counterC);
case 'D':case 'd':numC+=13*pow(base,counterC);
case 'E':case 'e':numC+=14*pow(base,counterC);
case 'F':case 'f':numC+=15*pow(base,counterC);
}

break;


default:
cout<<"sorry invalid base"<<endl<<endl;

}

return 0;
}


its too long maybe its possible to write it shorter but you know i am just a beginner in the programming world ...


User is offlineProfile CardPM
+Quote Post

Louisda16th
RE: HEXA To DEC
29 Nov, 2007 - 07:01 AM
Post #4

 101010101
Group Icon

Joined: 3 Aug, 2006
Posts: 1,812



Thanked: 1 times
Dream Kudos: 755
My Contributions
QUOTE
i have to work with numbers and letters which mean i have to use string ... and the main problem with the string how can i deal with each digit if its a string ?

Store the input in a string. Extract each digit one by one. when you get to letters, just store their equivalent numbers:
CODE

if digit is 0 then store 0
.
.
.
else
if digit is 'a' or 'A' then store 10
.
.
.

Hope this helps. smile.gif
User is offlineProfile CardPM
+Quote Post

RauLiTo
RE: HEXA To DEC
29 Nov, 2007 - 07:16 AM
Post #5

New D.I.C Head
*

Joined: 28 Nov, 2007
Posts: 19


My Contributions
yeah thats the idea ... but the problem is how to get each digit individually ?

for example : 2A

how can i get the A without the 2 ? and if i got it then i can switch the A with 10 ... as u said !


User is offlineProfile CardPM
+Quote Post

RauLiTo
RE: HEXA To DEC
29 Nov, 2007 - 10:37 AM
Post #6

New D.I.C Head
*

Joined: 28 Nov, 2007
Posts: 19


My Contributions
guys ?!
User is offlineProfile CardPM
+Quote Post

Bench
RE: HEXA To DEC
29 Nov, 2007 - 04:10 PM
Post #7

D.I.C Addict
Group Icon

Joined: 20 Aug, 2007
Posts: 686



Thanked: 24 times
Dream Kudos: 150
Expert In: C/C++

My Contributions
QUOTE(RauLiTo @ 29 Nov, 2007 - 01:52 PM) *

hi guys ... i want to write a program to convert a hexadecimal number to a decimal number using c++ ... can anyone help me in writing that program?

help me please biggrin.gif icon_up.gif

I'm not sure if you're doing this as part of an assignment, or just as a challenge, but there's an extremely short, simple way of doing this in C++.
CODE

#include <iostream>
#include <sstream>

int main()
{
    std::stringstream ss("10A");
    int num;

    ss >> std::hex >> num;
    std::cout << num << std::endl;
}


stringstreams are like FIFO string buffers. They work much in the same way as cin and cout (except to a memory buffer rather than a standard I/O device). you can add strings/numbers/etc using the << operator, and retrieve things with the >> operator.
User is offlineProfile CardPM
+Quote Post

RauLiTo
RE: HEXA To DEC
29 Nov, 2007 - 11:14 PM
Post #8

New D.I.C Head
*

Joined: 28 Nov, 2007
Posts: 19


My Contributions
Thanks alot Bench ... believe me i am just trying to do it as a challenge ...
in fact i dont get what you really mean ... you know i am just a beginner :$ !

in M.excel there are functions called left and right ...

for example :
4rty

left("the cell name " ; 2 )
which will give me : 4r

right("the cell name " ; 1)
which will give me : y

arent there the same functions in c++ ?!

This post has been edited by RauLiTo: 29 Nov, 2007 - 11:15 PM
User is offlineProfile CardPM
+Quote Post

foohoo
RE: HEXA To DEC
30 Nov, 2007 - 02:37 AM
Post #9

New D.I.C Head
*

Joined: 9 Aug, 2007
Posts: 30


My Contributions
You can think of Strings as an array of chars. So if the user has enetered an argument and you have stored it as a string then the string will have a length and you will be able to use a for loop to run through the string taking the each letter at a time:

CODE

String word;

word = "hello";

cout<< word[0];



would produce "h".

Hope that helps!

Foo

This post has been edited by foohoo: 30 Nov, 2007 - 02:38 AM
User is offlineProfile CardPM
+Quote Post

RauLiTo
RE: HEXA To DEC
1 Dec, 2007 - 10:53 AM
Post #10

New D.I.C Head
*

Joined: 28 Nov, 2007
Posts: 19


My Contributions
thaaaaaaaaanks alot mr foofoo biggrin.gif
thats exactly what i am looking for ... i wrote my program and everything fine now biggrin.gif thanks all of you
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/9/09 05:36AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month