new to C - decimal to hexadecimal converter

This is a discussion on new to C - decimal to hexadecimal converter within the C Programming forums, part of the General Programming Boards category; hi, I'm new to C programming and I'm still very unsure of many things. I tried making a decimal to ...

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    11

    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??

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,590
    > 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.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Jan 2010
    Posts
    11
    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

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,590
    > 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.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    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.

  6. #6
    Registered User
    Join Date
    Jan 2010
    Posts
    11
    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 11:35 AM.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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;
    }

  8. #8
    Registered User
    Join Date
    Jan 2010
    Posts
    11
    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

  9. #9
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    That's not how unions work. I suggest you read this:

    Union Declarations (C)

    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.

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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.

  11. #11
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    C99 has a %A specifier that will print a double as hex.

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    %A eh? C99 is sounding better all the time. Thanks Memloop, for remembering to keep me in the loop.

    Did I say that?

  13. #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"

  14. #14
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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 12:51 PM.

  15. #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...

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Hex String to Decimal converter
    By wrex in forum C Programming
    Replies: 16
    Last Post: 11-05-2008, 05:06 PM
  2. Decimal to Hex Converter
    By rocketman03 in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:50 AM
  3. Decimal to hexadecimal conversion
    By Nathalie in forum C Programming
    Replies: 9
    Last Post: 12-11-2007, 02:29 PM
  4. would you explain to me? hexadecimal to decimal
    By shteo83 in forum C Programming
    Replies: 2
    Last Post: 02-25-2006, 02:55 PM
  5. decimal to binary, decimal to hexadecimal and vice versa
    By Unregistered in forum C++ Programming
    Replies: 9
    Last Post: 12-08-2001, 10:07 PM

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