Write code for atoi(x) where x is hexadecimal string.
Answer Posted / mohammed sardar
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]>='f') && (x[n-1]<='f'))
sum+=(x[n-1]-'a'+10)<<leftshift;
n--;
leftshift+=4;
}
| Is This Answer Correct ? | 10 Yes | 7 No |
Post New Answer View All Answers
Is malloc memset faster than calloc?
What is structure padding and packing in c?
What does %p mean?
Are enumerations really portable?
printf(), scanf() these are a) library functions b) userdefined functions c) system functions d) they are not functions
Can you please explain the scope of static variables?
What does c mean in standard form?
What is the sizeof () operator?
Explain the difference between call by value and call by reference in c language?
Do you know what are bitwise shift operators in c programming?
What are header files why are they important?
Why we use stdio h in c?
State the difference between realloc and free.
Why c is faster than c++?
What should malloc() do?