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);

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.

Attachments error.jpg 33.34 KB
7 Years
Discussion Span
Last Post by Ancient Dragon

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.