Solved

Converting from decimal to hexadecimal

Posted on 2002-03-17
8
360 Views
Last Modified: 2013-11-20
how can i convert a decimal number to a hexadecimal number.
0
Comment
Question by:raqadi
[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
  • 5
  • 3
8 Comments
 

Expert Comment

by:rpatna
ID: 6873674
hi  raqadi,
following are my solution, if this solution not match with your question then plz. clear the question.
solution 1.
char hexaNo[10];
sprintf(hexaNo,"%x",n); // where n is Decimal no.

solution 2.
char hextable[16] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
#define TOHEX(a, b)    
       {*b++ = hextable[a >> 4];*b++ = hextable[a&0xf];}

solution 3.

char hexatable[]={'0','1','2','3','4',
             '5','6','7','8','9','A','B','C','D','E','F'};
int n;
char hexaNo[10];
scanf("%d",&n);
while(n)
{
     hexaNo[i]=hexatable[n%16];
      n = n/16;
}
printf("hexa no = %s",hexaNo);


0
 

Expert Comment

by:GEliyahu
ID: 6873676
This will explains you everything...
check it out..
http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=3209&lngWId=3
0
 

Expert Comment

by:GEliyahu
ID: 6873682
Or....


#include <iostream.h>


    void digittohex(int n){
    if (n < 10)
    cout << n;


        else{
        if (n == 10)
        cout << "A";
        if (n == 11)
        cout << "B";
        if (n == 12)
        cout << "C";
        if (n == 13)
        cout << "D";
        if (n == 14)
        cout << "E";
        if (n == 15)
        cout << "F";
    }
}


    void decimaltohex(int n){


        if (n >= 0 && n < 256){
        digittohex(n / 16);
        digittohex(n % 16);
    }
}


    int main(){
    int n;
    cin >> n;
    decimaltohex(n);
    cout << endl;
    return 0;
}
 

 
 
Report Bad Submission
Use this form to notify us if this entry should be deleted (i.e contains no code, is a virus, etc.).
Reason:  
 
Your Vote!

What do you think of this code(in the Beginner category)?
(The codewith your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor    
See Voting Log  
 
Other User Comments

 There are no comments on this submission.
 
 
Add Your Feedback!
Note:Not only will your feedback be posted, but an email will be sent to the code's author in your name.

NOTICE: The author of this code has been kind enough to share it with you.  If you have a criticism, please state it politely or it will be deleted.

For feedback not related to this particular code, please click here.
   
   Name:  
Comment:  
 

 
0
Industry Leaders: 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!

 

Accepted Solution

by:
GEliyahu earned 50 total points
ID: 6873685
Or....


#include <iostream.h>


    void digittohex(int n){
    if (n < 10)
    cout << n;


        else{
        if (n == 10)
        cout << "A";
        if (n == 11)
        cout << "B";
        if (n == 12)
        cout << "C";
        if (n == 13)
        cout << "D";
        if (n == 14)
        cout << "E";
        if (n == 15)
        cout << "F";
    }
}


    void decimaltohex(int n){


        if (n >= 0 && n < 256){
        digittohex(n / 16);
        digittohex(n % 16);
    }
}


    int main(){
    int n;
    cin >> n;
    decimaltohex(n);
    cout << endl;
    return 0;
}
 

 
0
 

Expert Comment

by:rpatna
ID: 6873714
hi GEliyahu

what u written in the last of second comment?
can i know ur mail-id plz?
0
 

Expert Comment

by:GEliyahu
ID: 6873761
first..
ignore pls the second comment...
just reffer to the third one..

second..
what is a mail-id?
you wants my E-mail?


0
 

Expert Comment

by:rpatna
ID: 6873797
yes plz.
0
 

Expert Comment

by:GEliyahu
ID: 6873808
geliyahu@hotmail.com
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

Title # Comments Views Activity
repeatSeparator  java  challenge 13 59
Creating Labels and Frequency list style reports in SAS code 9 129
Expand data scrubbing tool 13 36
Change to event 1 136
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

732 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