Solved

Converting decimal to hex and vice-versa

Posted on 2001-08-20
13
1,644 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
  • 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
 
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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 200 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

706 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now