Thread: new to C - decimal to hexadecimal converter

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Adak
    I should add that my compiler absolutely won't do it. I've tried several times already.
    What code did you try, and what was the output?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

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

  3. #18
    Registered User
    Join Date
    Jan 2010
    Posts
    11
    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
    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 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;
    }
    like i said when i input 13 it produces 41500000, weird

  4. #19
    Registered User
    Join Date
    Jan 2010
    Location
    Germany, Hannover
    Posts
    15
    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.

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

  6. #21
    Registered User
    Join Date
    Jan 2010
    Posts
    11
    I'm idiot, i finally got it working, its still a little off but its not a big deal, thanks

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, 06: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, 03:29 PM
  4. would you explain to me? hexadecimal to decimal
    By shteo83 in forum C Programming
    Replies: 2
    Last Post: 02-25-2006, 03: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, 11:07 PM