How to convert string to hex value in C -


i have string "6a" how can convert hex value 6a?

please me solution in c

i tried

char c[2]="6a" char *p; int x = atoi(c);//atoi deprecated   int y = strtod(c,&p);//returns first digit,rest considers string , //returns 0 if first character non digit char. 

the question

"how can convert string hex value?"

is asked, it's not quite right question. better

"how can convert hex string integer value?"

the reason is, integer (or char or long) value stored in binary fashion in computer.

"6a" = 01101010 

it in human representation (in character string) value expressed in 1 notation or another

"01101010b"   binary "0x6a"        hexadecimal "106"         decimal "'j'"         character 

all represent same value in different ways.

but in answer question, how convert hex string int

char hex[] = "6a";                          // here hex string int num = (int)strtol(hex, null, 16);       // number base 16 printf("%c\n", num);                        // print char printf("%d\n", num);                        // print decimal printf("%x\n", num);                        // print hex 

output:

j 106 6a 

Popular posts from this blog