Click to See Complete Forum and Search --> : Conversion Problems


Joshua
April 22nd, 1999, 02:36 PM
I'm working on a calculator type program. I want to add HEX, OCT, and BIN to change my answer into whichever button i press. What I'm trying to do is the conversion for these to happen. Since the whole program is written in C, I can't seem to do:
subtotal = bin(tempVal);
this ends up giving me 3 errors. I know that in C++ you can easily do:
cout<<hex<<tempVal;
but that can't seem to happen.
anyone have any ideas on how to help?? I'm just looking for the line of code that will convert my answer in BIN, HEX, or OCT. I know all the code around that part, it's just this one line of code i can't seem to get! any suggestions? Thanx!!

Dan O'Brien
April 22nd, 1999, 03:15 PM
First of all, this isn't a UI question, and it certainly isn't advanced UI, but here goes.

You can use sprintf to create hexadecimal strings, but you're stuck when it comes to binary. Writing your own routine is pretty simple, though: divide your number by the base you want to represent--for binary, two, for hex, 16. Insert the modulus remainder into the target string, and repeat recursively with the quotient until your quotient is 0.