Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Converting decimal to hex and vice-versa

Posted on 2001-08-20
13
Medium Priority
?
1,872 Views
Last Modified: 2012-06-27
Are there any C++ functions or Windows functions that can be used to convert to hex and vice-versa?

For example if I have a variable whose value is 10 million how would I convert it to the hex equivalent, 989680?? And how would I convert back to decimal? I prefer standard C++ functions but will use windows or MFC functions if there is no other way.
0
Comment
Question by:aderounm
[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
  • 4
  • 4
  • 2
  • +3
13 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 6407856
To hex:

char acHex [ MAX_BUFFER];

int number = 1234;
sprintf ( acHex, "%x", number);

To dec:

3include <stdlib.h>
char* pszHex = "0xab124def";
char* p;
int number;

number = strtol ( pszHex, &pc, 16);
0
 
LVL 4

Expert Comment

by:AssafLavie
ID: 6407931
or, in standard C++ (not c):
{
     ostringstream oss;
     oss << hex << 123;
     cout << oss.str();
     oss.str("");
     oss << dec << 0x7b;
     cout << oss.str();
}
0
 
LVL 4

Expert Comment

by:AssafLavie
ID: 6407935
or, if you're just interested in printing:

cout << hex << 123 << dec << 123 << oct << 123;

etc.
0
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!

 
LVL 7

Expert Comment

by:peterchen092700
ID: 6408498
I once wrote a helper function to do somclever onterpretation of numbers entered (various hex markers allowed, etc..) see
http://buerger.metropolis.de/bitbucket/controls/moreorless.html#HexDec%20Converter , perhaps it helps yu
0
 
LVL 86

Expert Comment

by:jkr
ID: 6409617
Hmm, ich mag besonders den 'Netz gegen Rechts'-Link auf der Hauptseite :o)
0
 
LVL 7

Expert Comment

by:peterchen092700
ID: 6409684
If this is the best of the site... well, perhaps I should change my profession ;-)
0
 
LVL 86

Expert Comment

by:jkr
ID: 6409702
Nicht 'das beste' - aber es freut mich immer, das zu sehen :o)
0
 
LVL 4

Expert Comment

by:AssafLavie
ID: 6409745
Medabrim li begermanit, ah? Walla yofi...

Ya'll start speaking in a manner which everyone can understand, alright?

As an Israeli I find this misterious german dialog suspicious and scary!
(not really... :)

(well, a bit maybe...)
0
 
LVL 86

Expert Comment

by:jkr
ID: 6409783
>>As an Israeli I find this misterious german dialog
>>suspicious and scary!

So, to translate it (and take away the scaryness, maybe):

Peter has a 'The Net against Nazis' button/link on his page, and I was just expressing my appreciation for that...
0
 
LVL 4

Expert Comment

by:AssafLavie
ID: 6409795
< wheew >
0
 
LVL 1

Accepted Solution

by:
noescom earned 800 total points
ID: 6428112
For dec to hex you can simply use itoa:

char buffer[16];
itoa(255, buffer, 16);
// param1 = number to convert
// param2 = buffer to store the result in
// param3 = radix, which is 16 for hexadecimal numbers

For hex to dec i usually use this function:

int
HexToDec(const char *x, int radix) {
     unsigned int counter = 0;
     int result = 0;

     while (counter < strlen(x)) {
          result = result * radix;

          if ((x[counter] >= '0') && (x[counter] <= '9')) {
               result = result + (x[counter] - '0');
          } else {
               result = result + (toupper(x[counter]) - 'A') + 10;
          }

          counter++;
     }

     return result;
}

You call it like:

int i = HexToDec("ff"); // i contains 255 now

Hope this helps!
0
 
LVL 4

Expert Comment

by:VincentLawlor
ID: 6428218
Only if you pass it as follows

int i HexToDec("ff",16);

You forgot the radix.

Vin.
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 6447270
#include <stdlib.h>
#include <stdio.h>

// IntToHex:
     int n= 10000000;  // ten million
     char szBuf[20];
     sprintf( szBuf, "%X", n ); // eg, "989680"

// common to add leading 0s so it is eight chars:

     sprintf( szBuf, "%08.8X", n ); // eg, "00989680"

//IntToDec:
     // char szBuf[20];
     sprintf( szBuf, "%d", n ); // eg, "10000000"

//HexToInt:
     char* pEndedHere;
     int m= strtol(szBuf, &pEndedHere, 16);

//DecToInt:
     // char* pEndedHere;
     m= strtol(szBuf, &pEndedHere, 10);

-- Dan
0

Featured Post

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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

604 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