C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
Iron Speed
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
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  
Old March 28th, 2008, 12:44 PM
pete212 pete212 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2008
Posts: 6 pete212 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 58 m 7 sec
Reputation Power: 0
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. )

Reply With Quote
  #2  
Old March 28th, 2008, 04:56 PM
Bobidybob's Avatar
Bobidybob Bobidybob is offline
Contributing Abuser
Click here for more information
 
Join Date: Apr 2007
Location: Starkville, MS
Posts: 183 Bobidybob User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 Days 2 h 43 m 48 sec
Reputation Power: 2
Send a message via AIM to Bobidybob
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.
__________________

Reply With Quote
  #3  
Old March 28th, 2008, 06:07 PM
pete212 pete212 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2008
Posts: 6 pete212 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 58 m 7 sec
Reputation Power: 0
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.

Reply With Quote
  #4  
Old March 31st, 2008, 06:13 AM
MaHuJa MaHuJa is offline
Contributing User
Click here for more information.
 
Join Date: Dec 2007
Posts: 342 MaHuJa User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 15 h 1 m 39 sec
Reputation Power: 1
Send a message via Skype to MaHuJa Send a message via XFire to MaHuJa
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.

Reply With Quote
  #5  
Old March 31st, 2008, 06:30 AM
MaHuJa MaHuJa is offline
Contributing User
Click here for more information.
 
Join Date: Dec 2007
Posts: 342 MaHuJa User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 15 h 1 m 39 sec
Reputation Power: 1
Send a message via Skype to MaHuJa Send a message via XFire to MaHuJa
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.

Reply With Quote
  #6  
Old March 31st, 2008, 12:30 PM
pete212 pete212 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2008
Posts: 6 pete212 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 58 m 7 sec
Reputation Power: 0
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!

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > General - Very simple C help


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

 Free IT White Papers!
 
Accelerating Trading Partner Performance
One in five. That's how many partner transactions have at least one error. That is an amazing statistic, particularly given the extraordinary leaps in innovation across the global supply chain during the past two decades. Download this white paper to learn more.

 
Competing on Analytics
This Tech Analysis is designed to help identify characteristics shared by analytics competitors, and includes information about 32 organizations that have made a commitment to quantitative, fact-based analysis.

 
Cost Effective Scaling with Virtualization and Coyote Point Systems
An overview of the industry trend toward virtualization, how server consolidation has increased the importance of application uptime and the steps being taken to integrate load balancing technology with virtualized servers.

 
Five Checkpoints to Implementing IP Telephony
Implementation planning for IP PBX software and IP telephony has become vital as businesses replace discontinued legacy PBX phone systems. This informative whitepaper outlines five &quot;checkpoints&quot; for any implementation plan that will help make IP communications a successful proposition.

 
Hosted Email Security: Staying Ahead of New Threats
In the last two years, email has become a fierce battleground between the nefarious forces of spam and malware, and the heroes of messaging protection. The spam volumes increased alarmingly every month, bringing clever new forms of phishing and virus propagation attacks.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 

Iron Speed




© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway