I want to convert Decimal value as Hex format in C,
I am having Decimal value 22(0x16),i want to convert it as 0x22
How to convert it ?
Please help me.
I want to convert Decimal value as Hex format in C,
I am having Decimal value 22(0x16),i want to convert it as 0x22
How to convert it ?
Please help me.
The decimal value "22" converted to hexadecimal (assuming a widespread notation) is "0x16", not "0x22".
http://www.c4learn.com/c-programs/program-for-decimal-to-hexadecimal.html Try this piece of code. Maybe it will help.
#include <stdio.h>
int conv(int n){//n>=0;
int hex = 0;
int base = 1;
for(; n ; n /= 10, base *= 16){
hex += (n % 10) * base;
}
return hex;
}
int main() {
int dec = 22;
int hex = conv(dec);
printf("%#x\n", hex);//0x22
return 0;
}