Decimal to hexadecimal conversion

This is a discussion on Decimal to hexadecimal conversion within the C Programming forums, part of the General Programming Boards category; I am trying to convert a decimal number (max. 255 / 8 bits) to hexadecimal and binary form. For now ...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    24

    Decimal to hexadecimal conversion

    I am trying to convert a decimal number (max. 255 / 8 bits) to hexadecimal and binary form. For now only tried to run the hexadecimal one, but somehow it keeps crashing without any information after running it. Can anyone spot the mistake?

    // The result should in fact be put in a structure variable, and of course numbers > 9 should become letters. I can try that later, but if this doesn't work then any extra things won't either. Also not sure how to declare the variables properly, this is but one variant of the things I tried.

    Thanks in advance!

    Code:
    #include <stdio.h>
    
    struct hex1
    {
    unsigned result1;
    unsigned result2;
    };
    
    int main ()
    
    {
    struct hex1;
    int number;
    unsigned result1;
    unsigned result2;
    
    printf("enter a number\n");
    scanf("%d", & number);
    
    result1 = number / 16;
    result2 = number - (result1 * 16);
    
    printf("result %d%d\n",result1,result2);
    
    }

  2. #2
    Novice.
    Join Date
    Oct 2005
    Posts
    88
    Well the thing is that you need to instruct the program to wait before closing. This is how you do it:

    Code:
    #include <stdio.h>
    
    struct hex1
    {
    unsigned result1;
    unsigned result2;
    };
    
    int main ()
    
    {
    struct hex1;
    int number;
    unsigned result1;
    unsigned result2;
    
    printf("enter a number\n");
    scanf("&#37;d", &number);
    
    result1 = number / 16;
    result2 = number - (result1 * 16);
    
    printf("result %d%d\n",result1,result2);
    getchar();
    getchar();
    
    }
    The getchar(); waits from a push on the enter key from the user; you could also run it in a dosbox.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,742
    Perhaps the value of indentation would be a lesson worth learning.
    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.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,887
    It works just fine (though the result will be wrong for numbers > 16).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Nov 2007
    Posts
    24
    Yup this works, thanks!

    Now trying to convert them to letters, but you can't put those in integers so I made chars instead. However I think you need to use pointers for this (as it says invalid conversion when running it) and I am not sure how to do it.

    I used the previous code and added for example:

    Code:
    if (result1 = 10)
        {
        charresult1="A";    
        }
    
    if (result1 = 11)
        {
        charresult1="B";    
        }
    
    if (result1 = 12)
        {
        charresult1="C";    
        }
    
    if (result1 = 13)
        {
        charresult1="D";    
        }
    
    if (result1 = 14)
        {
        charresult1="E";    
        }
    
    if (result1 = 15)
        {
        charresult1="F";    
        }
    How could I get this to work without an error?

    Also, I'm guessing it is not possible to convert an integer directly to a char like this?

    Code:
    if (result1 < 10)
        {
        charresult1=result1;    
        }

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,887
    Not SURE what you're trying to do (because I can't see the variable declarations), but something = "something" is usually a bad idea.
    A char is defined as 'a'. If you need to copy strings, you would use strcpy.

    You would need a char array to store your result. Make sure it has room for all letters + 1 more for '\0'. To convert int to string, you can use sprintf.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Nov 2007
    Posts
    24
    In hexadecimal, numbers for 10 -> 16 are replaced by letters, which is what I am trying to achieve after using omnificient's corrected code and declaring charresult1 and charresult2 as char variables. But how/why using an array? And should I do something = 'something' instead? :/

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,887
    No. It seems you lack understanding of arrays and strings.
    This might help: http://www.cprogramming.com/tutorial/c/lesson9.html
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Apr 2006
    Posts
    2,053
    Quote Originally Posted by Nathalie View Post
    Yup this works, thanks!

    Now trying to convert them to letters, but you can't put those in integers so I made chars instead. However I think you need to use pointers for this (as it says invalid conversion when running it) and I am not sure how to do it.

    I used the previous code and added for example:

    Code:
    if (result1 = 10)
        {
        charresult1="A";    
        }
    
    if (result1 = 11)
        {
        charresult1="B";    
        }
    
    if (result1 = 12)
        {
        charresult1="C";    
        }
    
    if (result1 = 13)
        {
        charresult1="D";    
        }
    
    if (result1 = 14)
        {
        charresult1="E";    
        }
    
    if (result1 = 15)
        {
        charresult1="F";    
        }
    How could I get this to work without an error?

    Also, I'm guessing it is not possible to convert an integer directly to a char like this?

    Code:
    if (result1 < 10)
        {
        charresult1=result1;    
        }
    First of all, It's worth pointing out that there are library functions for converting integers to hex and vice versa. So while it this is good practice, in practical applications you would use those.

    Second, your goal should be to convert an integer digit to an ascii character that represents the hex digit. So yes, your result should be a char no matter what.

    You need to add '0' to your hex digits less than A so that they represent the characters 0-9. Similarly, you need to scale your alphabetical digits by 'A'-10 or 'a'-10.

    Lastly, characters are printed like this:
    printf("%c",hexDigit);
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  10. #10
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,742
    > if (result1 = 10)
    It's ==, not = for comparison.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 07-04-2008, 01:39 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  3. binary decimal conversion
    By eerok in forum C Programming
    Replies: 2
    Last Post: 01-24-2006, 09:51 PM
  4. Header File Question(s)
    By AQWst in forum C++ Programming
    Replies: 10
    Last Post: 12-23-2004, 11:31 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

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