954,498 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

converting decimal to hexadecimal

Hey guys I am currently writing a code that will take the user input of a number and convert it to its hexadecimal form with the use of Stack ADT. This is what I have so far and would appreciate any help.

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include "stack.h"

int main(void)
{
int num;
char alpha;
int* digit;
STACK* stack;

stack = createStack ();

printf("Please enter your integer (decimal): \n");
scanf("%d" , &num);

while(num > 0)
{
	digit = (int*) malloc (sizeof(int));
	*digit = num % 16;
	push (stack, digit);
	num = num/16;
}

while (!emptyStack (stack))
{
	digit = (int*)popStack (stack);
	
	if(*digit > 9)
	{
		if(*digit == 10)
		alpha = 'A';
		else if(*digit == 11)
		alpha = 'B';
		else if(*digit == 12)
		alpha = 'C';
		else if (*digit == 13)
		alpha = 'D';
		else if (*digit == 14)
		alpha = 'E';
		else if (*digit == 15)
		alpha = 'F';
	}
	
	printf("%c", alpha);

	else
		printf("%d", digit);	
}

destroyStack(stack);
return 0;
}
Prisms
Light Poster
27 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
 
Hey guys I am currently writing a code that will take the user input of a number and convert it to its hexadecimal form with the use of Stack ADT. This is what I have so far and would appreciate any help.


With what? It's helpful to explainin full what the problem is rather than make us guess.

WaltP
Posting Sage w/ dash of thyme
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: