hi friends,
This is my very 1st message on this site. Can someone please help me out in designing a small function in c programming to accept decimal value as hex value, i mean lets say if user inputs a decimal value say 49, the processor should read(and futher processes) it as 0x49(hex value).

Thanks .

With regards

First of all you do what you want, the processor will always treate any data as Binary i.e. 0101010010001 etc. It does not read hexa numbers. Any way if want your wish to be true then start developing your own Processor.

thanks for replying this bull shit Luckychap. First read properly what i have written then reply with your bull shit. I said i want to convert 49 decimal to 0x49 hex (or 1001001 in binary). normally proccessor would accept the 49 decimal value as 0x31 hex value. (or 110001 in binary).........Got it !!!!!!!!!!!!

do you want to convert to a string? sprintf() can do that.

char hexval[8] = {0};
int n = 49;

sprintf(hexval,"0x%X", n);

Thanks Ancient Dragon for replying. Actually i'm using a 8-bit microcontroller AT89c51 to design a project . Program Memory of this controller is very low only 4kB. I tried using sprintf but it consumed more than 2kb itself. can there be a easier method for the same. Thanks once again,
Regards.

I don't think C language is the best for your situation. With such small amount of memory you may need to use assembly. Depending on your compiler C's startup code can be fairly large.

Here is a thread that may help you.

Here is a solution -- taken from that link I provided

char DecimalToHexa(int rem);
void hexaDecimal(int h, char* buffer, int bufferSize);
 
int main()	
{
	char buffer[8] = {0};
	int deci=115;
	hexaDecimal(deci, buffer, sizeof(buffer));
	printf("buffer = '%s'\n", buffer);
	return 0; 
}
 
void hexaDecimal(int h, char * buffer, int bufferSize)
{
    int rem;
    char output[8]= {0};
    char digit = 0;
    int i = 0;
    int j = 0;
    int k = 0;
    	
    do
    {    		
        rem = h%16;
        digit = DecimalToHexa(rem);
        h=h/16;
        output[i] = digit;
        ++i;
    } while(h/16!=0);
    if(h > 0)
	{
        rem = h;
        digit=DecimalToHexa(rem);   	
        output[i] = digit;
        ++i;
    }
    if(i >= bufferSize)
    {
        buffer[0] = 0;
    }
    else
    {
        for(j= i-1, k = 0; j >= 0; j--,k++)
        {
            buffer[k] = output[j];
        }
        buffer[i] = 0;
    }
}
 
    
char DecimalToHexa(int rem)
{		
	char c = 0;
	if( rem >= 10)
	{
		c = (rem - 10) + 'A';
	}
	else
		c = rem + '0';
	return c;
}
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.