Solved

Converting decimal to hex and vice-versa

Posted on 2001-08-20
13
1,778 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
Independent Software Vendors: 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 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

Independent Software Vendors: 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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

690 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