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
What functions are used for dynamic memory allocation in c language?
What is difference between static and global variable in c?
regarding pointers concept
What is dynamic memory allocation?
Write a program to display all the prime nos from 1 to 1000000, your code should not take time more than a minute to display all the nos.
What does typedef struct mean?
What is function prototype in c language?
Is struct oop?
How to get string length of given string in c?
Why is it important to memset a variable, immediately after allocating memory to it ?
What are the standard predefined macros?
What is volatile, register definition in C
What is wrong with this statement? Myname = 'robin';
What is property type c?
program to find error in linklist.(i.e find whether any node point wrongly to previous nodes instead of next node)