Write code for atoi(x) where x is hexadecimal string.
Answer Posted / john huang
int n=strlen(x) // where x is pointer to hex string
int sum=0;
int leftshift=0;
while(n>0)
{
if((x[n-1]>='0') && (x[n-1]<='9'))
sum+=(x[n-1]-'0')<<leftshift;
if((x[n-1]>='A') && (x[n-1]<='F'))
sum+=(x[n-1]-'A'+10)<<leftshift;
if((x[n-1]>='a') && (x[n-1]<='f'))
sum+=(x[n-1]-'a'+10)<<leftshift;
n--;
leftshift+=4;
}
| Is This Answer Correct ? | 7 Yes | 3 No |
Post New Answer View All Answers
Why we write conio h in c?
The file stdio.h, what does it contain?
What is the Purpose of 'extern' keyword in a function declaration?
What is linear search?
Difference between exit() and _exit() function?
List the variables are used for writing doubly linked list program.
Explain how to reverse singly link list.
What is string constants?
How can I do serial ("comm") port I/O?
Explain the bubble sort algorithm.
Explain the advantages and disadvantages of macros.
What is assignment operator?
How to delete a node from linked list w/o using collectons?
What is default value of global variable in c?
How many main () function we can have in a project?