What code did you try, and what was the output?Originally Posted by Adak
What code did you try, and what was the output?Originally Posted by Adak
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
It is a very old one - but I love it. That's why I posted "In my experience", earlier.
C99 is sounding like something that would be really a step up, I must say.
Edit:
I was working from the OP's program after touching it up, using a float data type from "myUnion.b".
Then I tried it with the integer in "myUnion.a".
Since neither were converted to hex, I thought the %x wouldn't convert anything else, either.
But it was the union that was goofing things up. Integers will be converted to hex values by %x. Floats will not, and unions can throw a monkey wrench into the change.
Output I recall was 0000.
Last edited by Adak; 01-23-2010 at 02:31 PM.
hmmm, maybe i should try a different compiler, but i can't find a good one for vista, like i tried codeblocks mingw release but nothing would build for some reason, ugh this is so frustrating...
anyway
here's my code after all the modifications
like i said when i input 13 it produces 41500000, weirdCode:#include <stdio.h> #include <stdlib.h> #include <string.h> int main() { union { int a; float b; }myUnion; printf("Enter the Decimal number to be converted:\n"); scanf("%f",&myUnion.b); printf("The number in Hexadecimal is:\n"); printf("%x",myUnion.a); while((myUnion.a = getchar()) != '\n'); //these two lines work to hold the console window open myUnion.a = getchar(); return 0; }
i get the same output, but its correct.
a float in memory has a different structure than an integer.
the printf("%x",int) means, take the 4 bytes at the ints address and treat them like they contain an int value but they don't.
I thought Memloop spelled out the trouble here:
new to C - decimal to hexadecimal converter
If you can give me an example of what you want, input and output. I'm sure I can help you get there. I will not use unions to do it, however. They are not required to do what you want and, (as you see here), can be a pain to work with.
You'll have to show me actual input and output or two or three, of what you want. I do not want a description only.
I'm idiot, i finally got it working, its still a little off but its not a big deal, thanks