C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 12-11-2007, 12:43 PM   #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);

}
Nathalie is offline   Reply With Quote
Old 12-11-2007, 01:01 PM   #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("%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.
omnificient is offline   Reply With Quote
Old 12-11-2007, 01:17 PM   #3
and the hat of Destiny
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 22,495
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.

Salem is offline   Reply With Quote
Old 12-11-2007, 01:17 PM   #4
Mysterious C++ User
 
Elysia's Avatar
 
Join Date: Oct 2007
Posts: 16,078
It works just fine (though the result will be wrong for numbers > 16).
__________________
Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2010 Ultimate, C++0x
"Thanks Elysia. You're a programming master! How the hell do you know every thing?"
"Thanks for all your help. It's obvious yall really know what you're talking about when it comes to OOP/C++ stuff."
Quoted... at least once.
Quote:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Old 12-11-2007, 01:52 PM   #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;    
    }
Nathalie is offline   Reply With Quote
Old 12-11-2007, 01:56 PM   #6
Mysterious C++ User
 
Elysia's Avatar
 
Join Date: Oct 2007
Posts: 16,078
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.
__________________
Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2010 Ultimate, C++0x
"Thanks Elysia. You're a programming master! How the hell do you know every thing?"
"Thanks for all your help. It's obvious yall really know what you're talking about when it comes to OOP/C++ stuff."
Quoted... at least once.
Quote:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Old 12-11-2007, 02:02 PM   #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? :/
Nathalie is offline   Reply With Quote
Old 12-11-2007, 02:07 PM   #8
Mysterious C++ User
 
Elysia's Avatar
 
Join Date: Oct 2007
Posts: 16,078
No. It seems you lack understanding of arrays and strings.
This might help: http://www.cprogramming.com/tutorial/c/lesson9.html
__________________
Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2010 Ultimate, C++0x
"Thanks Elysia. You're a programming master! How the hell do you know every thing?"
"Thanks for all your help. It's obvious yall really know what you're talking about when it comes to OOP/C++ stuff."
Quoted... at least once.
Quote:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Old 12-11-2007, 02:10 PM   #9
Registered User
 
Join Date: Apr 2006
Posts: 1,337
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.
King Mir is offline   Reply With Quote
Old 12-11-2007, 03:29 PM   #10
and the hat of Destiny
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 22,495
> 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.

Salem is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
[Assembly] Binary to decimal conversion for 64bit> numbers 1veedo Tech Board 3 07-04-2008 12:39 PM
Screwy Linker Error - VC2005 Tonto C++ Programming 5 06-19-2007 02:39 PM
binary decimal conversion eerok C Programming 2 01-24-2006 09:51 PM
Header File Question(s) AQWst C++ Programming 10 12-23-2004 11:31 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:04 AM.


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