• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1947
  • Last Modified:

Converting decimal to hex and vice-versa

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
aderounm
Asked:
aderounm
  • 4
  • 4
  • 2
  • +3
1 Solution
 
jkrCommented:
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
 
AssafLavieCommented:
or, in standard C++ (not c):
{
     ostringstream oss;
     oss << hex << 123;
     cout << oss.str();
     oss.str("");
     oss << dec << 0x7b;
     cout << oss.str();
}
0
 
AssafLavieCommented:
or, if you're just interested in printing:

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

etc.
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
peterchen092700Commented:
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
 
jkrCommented:
Hmm, ich mag besonders den 'Netz gegen Rechts'-Link auf der Hauptseite :o)
0
 
peterchen092700Commented:
If this is the best of the site... well, perhaps I should change my profession ;-)
0
 
jkrCommented:
Nicht 'das beste' - aber es freut mich immer, das zu sehen :o)
0
 
AssafLavieCommented:
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
 
jkrCommented:
>>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
 
AssafLavieCommented:
< wheew >
0
 
noescomCommented:
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
 
VincentLawlorCommented:
Only if you pass it as follows

int i HexToDec("ff",16);

You forgot the radix.

Vin.
0
 
DanRollinsCommented:
#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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 4
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now