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

Join 136,465 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,571 people online right now. 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
30 Jun, 2008 - 09:55 AM
Post #1

New D.I.C Head
*

Joined: 30 Jun, 2008
Posts: 3



Thanked: 1 times
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
+Quote Post

AmitTheInfinity
RE: Conversion Decimal To Hex Or Vice Versa In C Only Format
30 Jun, 2008 - 09:27 PM
Post #2

C Surfing ∞
Group Icon

Joined: 25 Jan, 2007
Posts: 1,025



Thanked: 35 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
+Quote Post

bladeknight1980
RE: Conversion Decimal To Hex Or Vice Versa In C Only Format
30 Jun, 2008 - 10:41 PM
Post #3

New D.I.C Head
*

Joined: 30 Jun, 2008
Posts: 3



Thanked: 1 times
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
+Quote Post

AmitTheInfinity
RE: Conversion Decimal To Hex Or Vice Versa In C Only Format
30 Jun, 2008 - 10:49 PM
Post #4

C Surfing ∞
Group Icon

Joined: 25 Jan, 2007
Posts: 1,025



Thanked: 35 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
+Quote Post

captainhampton
RE: Conversion Decimal To Hex Or Vice Versa In C Only Format
2 Jul, 2008 - 05:29 AM
Post #5

Jawsome++;
Group Icon

Joined: 17 Oct, 2007
Posts: 518



Thanked: 2 times
Dream Kudos: 825
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 - 05:29 AM
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/2/08 04:47PM

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