Google Answers Logo
View Question
 
Q: Decimal to Hex using c/c++ ( No Answer,   6 Comments )
Question  
Subject: Decimal to Hex using c/c++
Category: Computers
Asked by: raghu1979-ga
List Price: $5.00
Posted: 03 Nov 2005 10:38 PST
Expires: 04 Nov 2005 09:43 PST
Question ID: 588461
How to convert a decimal number to hexadecimal number using c/c++ programs ?

Is there any built in functions to convert from dec to hex in c/c++ ?
Answer  
There is no answer at this time.

Comments  
Subject: Re: Decimal to Hex using c/c++
From: jpariag-ga on 03 Nov 2005 11:13 PST
 
Try this page

http://mathforum.org/library/drmath/view/54347.html

jpariag-ga
Subject: Re: Decimal to Hex using c/c++
From: athar123-ga on 03 Nov 2005 14:37 PST
 
if you have a number stored in a variable int (or its mutent type)
then it is stored in binary format. So when you print to screen (or
file) at that time you can also specify in which base you want the
number to show up. You can specify %d for decimal and %x for
hexadecimal.

You can also use itoa(...) function to convert the number to string
and specify the base in it. In this case base can be over wide range
including decimal and hexadecimal.
Subject: Re: Decimal to Hex using c/c++
From: raghu1979-ga on 03 Nov 2005 15:21 PST
 
Hi,

I have used _itoa() to convert the integer to string of base16.

But I need some result in 0X2BCF kind of format so that I can assign
it directly to a variable of type DWORD.

For example, If I pass 12345 to a function, it should return me 0x3039.
Is it possible ?
Subject: Re: Decimal to Hex using c/c++
From: efn-ga on 03 Nov 2005 23:50 PST
 
A number of an integer type is just stored as a bunch of bits in the
computer.  Decimal and hexadecimal representations only get involved
when you need to convert to character string representation for
interfacing with people.  For convenience, the C and C++ languages
allow you to write a number either in decimal or in hexadecimal
notation with the "0x" prefix, but these notations do not represent
different kinds of values.  A DWORD is an integer type, so it just
deals with values, not character representations.  So you can assign
12345 or 0x3039 to a DWORD and you will get the same result either
way, because those are just two different ways of writing the same
number in the programming language.  So the function to convert 12345
to 0x3039 would look like this:

DWORD dontdoanything(DWORD input)
{
    return input;
}

Now if you are asking about getting a hexadecimal *character*
representation of a value, that is also possible, but from your last
comment, it doesn't look like that is your problem.
Subject: Re: Decimal to Hex using c/c++
From: aminvikas-ga on 04 Nov 2005 03:03 PST
 
As said by  efn-ga,A number of an integer type is just stored as a
bunch of bits in the computer.  Decimal and hexadecimal are the
representations  used by the
user's for  better understanding . 
We can surely write a Functin for doing the task u explained, but i need to know
exact details for the functions .We cannot store character data 0x330C in a DWord.
Thank you
Subject: Re: Decimal to Hex using c/c++
From: bradgoodman-ga on 04 Nov 2005 08:21 PST
 
int main() {
char outStr[256];
int inDec = 65535;
sprintf(outStr,"%x",inDec);
printf("%d in hex is %s\n",inDec,outStr);
}

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy