Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 119,729 C++ Programmers for FREE! Ask your question and get quick answers from experts. There are 1,324 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



Conversion Decimal to hex or vice versa in C only format

 
Reply to this topicStart new topic

Conversion Decimal to hex or vice versa in C only format

bladeknight1980
post 30 Jun, 2008 - 10:55 AM
Post #1


New D.I.C Head

*
Joined: 30 Jun, 2008
Posts: 3


My Contributions


cpp

#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>
#include<string.h>


void DEXTOHEX(long int jem);
long int jem;
long int temp,temp2;
char choice;
main()
{
clrscr();
gotoxy(5,3);printf("================================================================");
gotoxy(5,4);printf("=== ===");
gotoxy(5,5);printf("=== [A] convert decimal to binary and vice versa. ===");
gotoxy(5,6);printf("=== [B] convert decimal to octal and vice versa. ===");
gotoxy(5,7);printf("=== [C] convert decimal to hexadecimal and vice versa. ===");
gotoxy(5,8);printf("=== [D] convert binary to octal and vice versa. ===");
gotoxy(5,9);printf("=== [E] convert binary to hexadecimal and vice versa. ===");
gotoxy(5,10);printf("=== [F] convert octal to hexadecimal and vice versa. ===");
gotoxy(5,11);printf("=== ===");
gotoxy(5,12);printf("=== Enter Choice [ ] ===");
gotoxy(5,13);printf("================================================================");
gotoxy(26,12);choice=getche();
clrscr();


switch(choice)
{

case 'A':
case 'a':
{
clrscr();
printf("Convert Decimal to Binary and vice versa");
gotoxy(5,10);printf("Enter the Decimal Number:");
gotoxy(30,10);scanf("%ld",&jem);
break;
}
case 'B':
case 'b':
{
clrscr();
printf("Convert Decimal to Octal and Vice Versa");
gotoxy(5,10);printf("Enter the Decimal Number:");
gotoxy(30,10);scanf("%ld",&jem);
break;
}
case 'C':
case 'c':
{
clrscr();
printf("Convert Decimal to Hexadecimal and Vice Versa");
gotoxy(5,10);printf("Enter the Decimal Number:");
gotoxy(30,10);scanf("%ld",&jem);
DECTOHEX(jem);
break;
}

default:
{
clrscr();
printf("under construction");
}
}
getch();
}
void DECTOHEX(long int jem)
{
long int bin = 0;
int b,p=0;
temp=jem;
while (jem>0)
{
b=jem%16;
jem=jem/16;
bin=bin+b*pow(10,p++);
}
gotoxy(5,15);printf("The Hexadecimal Equivalent of %ld is %c",temp,bin);

}

.....I am having trouble with this one. 10 in decimal should be A in hex but not. pls help blink.gif

** Edit ** code.gif
User is offlineProfile CardPM

Go to the top of the page


AmitTheInfinity
post 30 Jun, 2008 - 10:27 PM
Post #2


D.I.C Addict

Group Icon
Joined: 25 Jan, 2007
Posts: 868



Thanked 24 times

Dream Kudos: 125
My Contributions


With your logic it will print 10 when the reminder is 10. Your logic worked for binary and octal as they are smaller systems than decimal. Hex is bigger than decimal and involves numbers like A,B,C,D,E,F so you have take special measures to handle them. Whenever you get reminder as 10 or more you have to replace it with appropriate hex equivalent in your final answer, if you are printing it all at once then you might need to store these values in array and then print them. If you want to print the hex then you have to print it digit by digit or by coping the answer in character array and printing it.
User is offlineProfile CardPM

Go to the top of the page

bladeknight1980
post 30 Jun, 2008 - 11:41 PM
Post #3


New D.I.C Head

*
Joined: 30 Jun, 2008
Posts: 3


My Contributions


QUOTE(AmitTheInfinity @ 30 Jun, 2008 - 10:27 PM) *

With your logic it will print 10 when the reminder is 10. Your logic worked for binary and octal as they are smaller systems than decimal. Hex is bigger than decimal and involves numbers like A,B,C,D,E,F so you have take special measures to handle them. Whenever you get reminder as 10 or more you have to replace it with appropriate hex equivalent in your final answer, if you are printing it all at once then you might need to store these values in array and then print them. If you want to print the hex then you have to print it digit by digit or by coping the answer in character array and printing it.



can u show me how? may be a simple functions? pls
User is offlineProfile CardPM

Go to the top of the page

AmitTheInfinity
post 30 Jun, 2008 - 11:49 PM
Post #4


D.I.C Addict

Group Icon
Joined: 25 Jan, 2007
Posts: 868



Thanked 24 times

Dream Kudos: 125
My Contributions


QUOTE(bladeknight1980 @ 1 Jul, 2008 - 12:11 PM) *

QUOTE(AmitTheInfinity @ 30 Jun, 2008 - 10:27 PM) *

With your logic it will print 10 when the reminder is 10. Your logic worked for binary and octal as they are smaller systems than decimal. Hex is bigger than decimal and involves numbers like A,B,C,D,E,F so you have take special measures to handle them. Whenever you get reminder as 10 or more you have to replace it with appropriate hex equivalent in your final answer, if you are printing it all at once then you might need to store these values in array and then print them. If you want to print the hex then you have to print it digit by digit or by coping the answer in character array and printing it.



can u show me how? may be a simple functions? pls



See Here...
User is offlineProfile CardPM

Go to the top of the page

captainhampton
post 2 Jul, 2008 - 06:29 AM
Post #5


D.I.C Addict

Group Icon
Joined: 17 Oct, 2007
Posts: 508



Thanked 2 times

Dream Kudos: 775
My Contributions


Not sure how true the conversions have to be to your code, however for the record here is a way to convert between bases:

CODE

// modify basefield
#include <iostream>
using namespace std;

int main () {
  int n;
  n=70;
  cout << hex << n << endl;
  cout << dec << n << endl;
  cout << oct << n << endl;
  return 0;
}


This post has been edited by captainhampton: 2 Jul, 2008 - 06:29 AM
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 10/15/08 04:31PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month