C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 01-23-2010, 11:44 AM   #1
Registered User
 
Join Date: Jan 2010
Posts: 9
new to C - decimal to hexadecimal converter

hi, I'm new to C programming and I'm still very unsure of many things.

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);
}
I tried to make it really simple, but whats wrong??
emblem4ever is offline   Reply With Quote
Old 01-23-2010, 11:47 AM   #2
and the hat of Jobseeking
 
Salem's Avatar
 
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
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.

Salem is offline   Reply With Quote
Old 01-23-2010, 11:54 AM   #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   Reply With Quote
Old 01-23-2010, 11:57 AM   #4
and the hat of Jobseeking
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,639
> Union
Try union

All C keywords are lowercase.
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.

Salem is offline   Reply With Quote
Old 01-23-2010, 11:59 AM   #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   Reply With Quote
Old 01-23-2010, 12:10 PM   #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   Reply With Quote
Old 01-23-2010, 12:42 PM   #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   Reply With Quote
Old 01-23-2010, 01:12 PM   #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   Reply With Quote
Old 01-23-2010, 01:18 PM   #9
Robot
 
Join Date: Mar 2009
Posts: 258
That's not how unions work. I suggest you read this:

Union Declarations (C)

Quote:
If a union of two types is declared and one value is stored, but the union is accessed with the other type, the results are unreliable. For example, a union of float and int is declared. A float value is stored, but the program later accesses the value as an int. In such a situation, the value would depend on the internal storage of float values. The integer value would not be reliable.
Memloop is offline   Reply With Quote
Old 01-23-2010, 01:18 PM   #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   Reply With Quote
Old 01-23-2010, 01:20 PM   #11
Robot
 
Join Date: Mar 2009
Posts: 258
C99 has a %A specifier that will print a double as hex.
Memloop is offline   Reply With Quote
Old 01-23-2010, 01:28 PM   #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   Reply With Quote
Old 01-23-2010, 01:45 PM   #13
Registered User
 
Join Date: Jan 2010
Location: Germany, Hannover
Posts: 15
Quote:
Originally Posted by Adak View Post
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.
no, printf can do this:
Code:
 printf("%x\n",15);
should give you a nice little "f"
kermitaner is offline   Reply With Quote
Old 01-23-2010, 01:48 PM   #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   Reply With Quote
Old 01-23-2010, 02:02 PM   #15
Registered User
 
Join Date: Jan 2010
Location: Germany, Hannover
Posts: 15
Quote:
Originally Posted by Adak View Post
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.
Code:
    int i=255;
  printf("%x\n",i);
even gives me 2 "ff"

maybe u should try another compiler...
kermitaner is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 12:37 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22