![]() |
| | #1 |
| Registered User Join Date: Nov 2007
Posts: 24
| Decimal to hexadecimal conversion // 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 | |
| | #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();
}
|
| omnificient is offline | |
| | #3 |
| and the hat of Destiny 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. |
| Salem is offline | |
| | #4 | |
| Mysterious C++ User 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:
| |
| Elysia is offline | |
| | #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";
}
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 | |
| | #6 | |
| Mysterious C++ User 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:
| |
| Elysia is offline | |
| | #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 | |
| | #8 | |
| Mysterious C++ User 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:
| |
| Elysia is offline | |
| | #9 | |
| Registered User Join Date: Apr 2006
Posts: 1,337
| Quote:
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 | |
| | #10 |
| and the hat of Destiny Join Date: Aug 2001 Location: The edge of the known universe
Posts: 22,495
| > if (result1 = 10) It's ==, not = for comparison. |
| Salem is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |