![]() |
| | #1 |
| Registered User Join Date: Jan 2010
Posts: 9
| new to C - decimal to hexadecimal converter I tried making a decimal to hexadecimal converter, using a very simple method, yet it doesn't seem to work and I can't figure out what syntax errors i have, heres my code Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void main
{
Union
{
int a;
float b;
}myUnion;
printf("Enter the Decimal number to be converted:\n");
scanf("%f",&myUnion.b\n);
printf("The number in Hexadecimal is:\n");
printf("%x",&myUnion.a);
}
|
| emblem4ever is offline | |
| | #2 |
| and the hat of Jobseeking Join Date: Aug 2001 Location: The edge of the known universe
Posts: 21,639
| > scanf("%f",&myUnion.b\n); Remove the \n here > printf("%x",&myUnion.a); Remove the & here > void main Remove the void here, and replace it with int |
| Salem is offline | |
| | #3 |
| Registered User Join Date: Jan 2010
Posts: 9
| oh, thanks a lot, however i still get many errors, but i really don't get why, like it says syntax error before "{" in line 11 and conflicting types for 'printf' etc. but according to my knowlegde and other programs i've seen, it should work fine...? maybe its my compiler, im using Dev-C 4.9.9.2 because im using vista.... hmmmmm |
| emblem4ever is offline | |
| | #4 |
| and the hat of Jobseeking Join Date: Aug 2001 Location: The edge of the known universe
Posts: 21,639
| > Union Try union All C keywords are lowercase. |
| Salem is offline | |
| | #5 |
| Robot Join Date: Mar 2009
Posts: 258
| C is case dependent, and the standard data type for unions is union and not Union. The %x modifier expects an integer, but you're passing it a float. I have no idea why you're storing the input in an union to begin with, let alone a float. |
| Memloop is offline | |
| | #6 |
| Registered User Join Date: Jan 2010
Posts: 9
| because i want to scan the input as a float, i.e. user enters 26.74628, then using the union, "cast" it as an int, so it can then be modified and displayed as a hexadecimal anyway, so i got it to compile, but when i run the .exe file, the command prompt opens up and I type in a decimal number but when i press ENTER the program just closes.....why would it do this?? Last edited by emblem4ever; 01-23-2010 at 12:35 PM. |
| emblem4ever is offline | |
| | #7 |
| Registered User Join Date: Sep 2006
Posts: 3,125
| This is your program with a few wrinkles. It runs fine, but you have NO logic to change the float, or the integer, into a hexadecimal number. Code: #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 float number in base 10 is: %f, and in Hexadecimal is: %x\n", myUnion.b, myUnion.a);
printf("%d",(int) myUnion.b); //NOTE the cast to an int
while((myUnion.a = getchar()) != '\n'); //these two lines work to hold the console window open
myUnion.a = getchar();
return 0;
}
|
| Adak is online now | |
| | #8 |
| Registered User Join Date: Jan 2010
Posts: 9
| thanks for all the help, but i think ur still misunderstanding what i want to do i want to input a float number, like -27.198EE33 (-27.198 x 10^33) then using the union to read it as an int (sorry for saying cast before) so it can be outputed in hexadecimal form (as already said the hex modifier only works on int values) so in this case it sould spit out F8A7EE1 but for some reason its not working properly, like if input 13, which is D in hexadecimal, i get 41500000 as an output |
| emblem4ever is offline | |
| | #9 | |
| Robot Join Date: Mar 2009
Posts: 258
| That's not how unions work. I suggest you read this: Union Declarations (C) Quote:
| |
| Memloop is offline | |
| | #10 |
| Registered User Join Date: Sep 2006
Posts: 3,125
| You're saying that the printf() format specification %x will automatically convert your number from a base 10 number, into a base 16 hex number. That has not been my experience. ![]() %x will print a hex number in the correct format. That is all it will do, AFAIK. If you want a number converted from base 10 to base 16, you do the conversion or use a standard function to do it for you. After the conversion, you can use %x or %X to print up the hex number. |
| Adak is online now | |
| | #11 |
| Robot Join Date: Mar 2009
Posts: 258
| C99 has a %A specifier that will print a double as hex. |
| Memloop is offline | |
| | #12 |
| Registered User Join Date: Sep 2006
Posts: 3,125
| %A eh? C99 is sounding better all the time. Thanks Memloop, for remembering to keep me in the loop. Did I say that? |
| Adak is online now | |
| | #13 | |
| Registered User Join Date: Jan 2010 Location: Germany, Hannover
Posts: 15
| Quote:
Code: printf("%x\n",15);
| |
| kermitaner is offline | |
| | #14 |
| Registered User Join Date: Sep 2006
Posts: 3,125
| 15 is not a variable. It is a hex number. Try that with an integer variable (base 10), and see who gets the "f". ![]() I should add that my compiler absolutely won't do it. I've tried several times already. Last edited by Adak; 01-23-2010 at 01:51 PM. |
| Adak is online now | |
| | #15 | |
| Registered User Join Date: Jan 2010 Location: Germany, Hannover
Posts: 15
| Quote:
Code: int i=255;
printf("%x\n",i);
![]() maybe u should try another compiler... | |
| kermitaner is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Hex String to Decimal converter | wrex | C Programming | 16 | 11-05-2008 06:06 PM |
| Decimal to Hex Converter | rocketman03 | C Programming | 1 | 10-25-2008 03:50 AM |
| Decimal to hexadecimal conversion | Nathalie | C Programming | 9 | 12-11-2007 03:29 PM |
| would you explain to me? hexadecimal to decimal | shteo83 | C Programming | 2 | 02-25-2006 03:55 PM |
| decimal to binary, decimal to hexadecimal and vice versa | Unregistered | C++ Programming | 9 | 12-08-2001 11:07 PM |