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
Can we assign integer value to char in c?
Do pointers store the address of value or the actual value of a variable?
Why can arithmetic operations not be performed on void pointers?
#include
given post order,in order construct the corresponding binary tree
how can use subset in c program and give more example
Do array subscripts always start with zero?
Tell me what is the purpose of 'register' keyword in c language?
Explain what is a pragma?
Differentiate between ordinary variable and pointer in c.
Why dont c comments nest?
How can a program be made to print the line number where an error occurs?
What are the output(s) for the following ? #include char *f() {char *s=malloc(8); strcpy(s,"goodbye")} main() { char *f(); printf("%c",*f()='A'); }
I came across some code that puts a (void) cast before each call to printf. Why?
Write a program, where i have a grid with many cells, how many paths are possible from one point to other desired points.