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
What is #line?
How are portions of a program disabled in demo versions?
What is the general form of #line preprocessor?
What is a null string in c?
where are auto variables stored? What are the characteristics of an auto variable?
Why do we use & in c?
What is "Hungarian Notation"?
What is malloc and calloc?
Explain how can I remove the trailing spaces from a string?
What is the scope of local variable in c?
How can I call fortran?
Create a structure to specify data on students as given below: Roll number, Name, Department, Course, and Year of joining. Assume that there are not more than 450 students in the collage. (a) Write a function to print the names of all students who joined in the last 3 years. (b) Write a function to print the data of a student whose roll numbers are divisible by 4.
What is define c?
The performance of an operation in several steps with each step using the output of the preceding step a) recursion b) search c) call by value d) call by reference
What extern c means?