| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
|
|
Free Web 2.0 Code Generator! Generate data entry and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!
|
|
#1
|
|||
|
|||
|
General - Very simple C help
Hi, i'm a newbie to C and currently trying to learn the language, so at the moment i only really know the basic stuff. I'm trying to create a program that converts a decimal number (entered by the user) into hexidecimal. I have got it working for one single, decimal digit, but i have hit a mental block and can't seem to see how i am able to do it for when the decimal number is above 15. Here is what i have so far:
#include <stdio.h> char hex; unsigned int decimal; //stores decimal number that needs to be converted. int main() { printf("Please enter the digit: "); scanf("%u", &decimal); if (decimal < 10) hex = '0' + decimal; else hex = 'A' + (decimal - 10); printf("%c ",hex); return 0; } If you enter 16 for example, it displays G (as i expected) but obviously it needs to loop round some how so that it displays 10. I'm thinking it would be a FOR loop but again, i can't see how it would work. Any help would be greatly appreciated! (Please try and keep it as simple as possible, some of the posts on here make my mind blow up. ) |
|
#2
|
||||
|
||||
|
int a;
int b; a = decimal%15 b = decimal/15; a would equal the ones place and b would equal the 10's place. this is only really helpful for 2 digit hex numbers though. you may want to use a while loop and some other logic for exiting the loop. iono, i think there was a guy who posted the same problem a while back and had a solution. try using the search engine, you may be delightfully surprised at what you find. ![]()
__________________
|
|
#3
|
|||
|
|||
|
Thanks for your reply. Just a quick question, shouldn't it be %16 and /16 to get the 1s and the 16s column? Because hex is a 16 base system? Or should it be 15 like you originally said? Sorry, that has just confused me a bit.
Also, how would that fit into the code i have already got? Tried searching, couldn't find the thread you were refering to. There's a lot on binary to hex but nothing on decimal to hex. Thanks in advance. ![]() |
|
#4
|
|||
|
|||
|
The simplest solution in C++ is along the lines of
int i; cin >> i; cout << hex << i; And if you don't want to output it quite yet (just convert it to a string), look into ostringstream. For C, I wouldn't be surprised if there are similar library functions you can use. |
|
#5
|
|||
|
|||
|
Code:
char hexdigit (int i); //Convert single digit, what you've already done
void hexit (char* dest, int input) // Assuming int to be max 32 bit. Should it be more, there are fans to clean.
{
char buf[10] = {0};
int i;
for (i=8; input; i--) {
buf[i] = hexdigit(input&0x0f); // Effectively identical to(input%16);
input = input >> 4; // Effectively identical to input/16
}
strcpy(dest,&buf[i]); // Everything bad you've heard about strcpy, applies here.
}
I haven't tested it, and in fact I haven't let a compiler loose on it. It's also some things I'm not too confident about remembering correctly. |
|
#6
|
|||
|
|||
|
Thanks for all your help. I've finally managed to do this after a couple of days, and came up something not to dis-similar to MaHuJa's!
![]() |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > General - Very simple C help |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|