# Help with Hexa conversion

• 12-13-2002
schism
Help with Hexa conversion
I've been trying to write a program that will accept two numbers from two different number systems (ie: Decimal, Hexa, Octa, or Binary)... Everything works except for the hexadecimal input... I know there's a way to set the output for an answer in hex, but is there any way to input a hexadecimal number from the keyboard, and convert it to decimal to add two numbers of a different base together?

Example: 10(base 10) + 4e2f(base 16) = ??

I really need to know this, anything would help...

Thanks
• 12-13-2002
sure, it's called a user defined function. In broad strokes accept input as a string. convert the values represented by each placeholder into the base system you wish to use. then convert the answer to whatever other base system you wish to use.
• 12-13-2002
schism
Ummm... Okay, maybe I should also add that I'm only as far as typedef in my Cpp class... We've done functions, but I don't know what a user-defined one is...
Pretty much covered everything up to and including arrays... That's about it.

Schism
• 12-13-2002
DougDbug
strtoul()
We use strtoul()

I don't know if it's ANSI, but it is included with the Microsoft Compiler. (And I forgot to look-up which library it's in.)

here's a code fragment from an existing C program:

Code:

```unsigned RtnValue, testdata, testdata2, datamask; char command[32], *ptr; gets(command);              // get command from kybd if(!strcmpi(command, "exit"))         return 1; if(!strlen(command))         datamask = 0xFFFF; else         datamask = (int)strtoul(command, &ptr, 16);  //**** strtoul() **** printf("Data mask = %04X\n", datamask);```

strtoul() is ANSI and it's in stdlib.h ...of all places!
[/edit/
• 12-14-2002
I have this problem of occassionally putting my foot in my mouth when I don't look things up I am not intimately familiar with before I post on the topic, but here goes anyway.

int input = ABCE;

where ABCE is a hexaddeicmal number, whatever it is, is likely to crash.

preceding the value with Ox, or whatever, may save you, but I haven't verified that.

using X in the format string of printf() doesn't really change the base of the output, it changes the presentation of the value.

Not every base is covered by printf() format string options or by stream modifiers/manipulators. Not that you use bases other than binary, octal, decimal, or hex very often, but nevertheless, if you want to be able to convert from any given base to any other, then the available stuff don't work all the time and you have to write it yourself.

functions you write yourself are called user defined functions.
• 12-14-2002
nextus
schism from redmoon?
is this schism from redmoon?
• 12-15-2002
swoopy
Try this.
Code:

```  int num;   cout << "Enter number in hex:";   cin >> hex >> num;   cout << "num:" << num << endl;   cout << "num:" << hex << num << endl;```