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

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. 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. Perhaps the value of indentation would be a lesson worth learning.

4. It works just fine (though the result will be wrong for numbers > 16).

5. 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. 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.

7. 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. No. It seems you lack understanding of arrays and strings.
This might help: http://www.cprogramming.com/tutorial/c/lesson9.html

9. Originally Posted by Nathalie
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);

10. > if (result1 = 10)
It's ==, not = for comparison.