-1

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.

Tim
  • 41,901
  • 18
  • 127
  • 145
Shiva
  • 165
  • 1
  • 2
  • 9
  • Ignoring that Decimal 22 is hex 0x16; What programming language are you using, and what data types are you working with? – Rowland Shaw Mar 18 '14 at 07:33
  • 1
    Could you clarify what value you are trying to express? Is it 22 in base 10 (0x16)? Or is it 34 in base 10 (0x22)? – Tony Mar 18 '14 at 08:33
  • possible duplicate of [How to convert decimal literals into hex in C, 500 to 0x0500](http://stackoverflow.com/questions/7448054/how-to-convert-decimal-literals-into-hex-in-c-500-to-0x0500) – Jongware Mar 18 '14 at 09:36

3 Answers3

0

The decimal value "22" converted to hexadecimal (assuming a widespread notation) is "0x16", not "0x22".

Codor
  • 17,447
  • 9
  • 29
  • 56
0

http://www.c4learn.com/c-programs/program-for-decimal-to-hexadecimal.html Try this piece of code. Maybe it will help.

Dragos
  • 296
  • 2
  • 4
  • 13
0
#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;
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70