So I converting a decimal value into hex, but I need to print it out from an array. I'm not sure where to go from here.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int hexa = 16, deci, quotient, p;

void dec2hex(unsigned int x, char hex[])
{
	for (p = 1; p<=x; p++)
		quotient = x / hexa;
	hex[32] = 0;
}

if (op == 'h')	
{
     dec2hex(dec, hex);
     printf("%s\n", hex);
     getche();
}

I left some parts out of course but I just need help with the hex section of my code. When I run it the binary section works just fine but hex prints out an empty array. Any help would be greatly appreciated.

Recommended Answers

All 2 Replies

im not sure why you were doing this:

quotient = x / hexa;

To convert from decimal to hex, try this:

1) Note remainder when divided by 16( i mean num modulo 16).
2) Divide number by 16(integer division)
3) Repeat steps 1 and 2 till you obtain a quotient of 0(in the second step).

Hope that helps.

move all those global variables on line 5 down below line 8 so that they are inside the function. Globals are bad -- avoid them when ever possible.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.