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 maximum size of array in c?
What is the Purpose of 'extern' keyword in a function declaration?
Whats s or c mean?
A global variable when referred to in another file is declared as this a) local variable b) external variable c) constant d) pointers
Explain what is the purpose of "extern" keyword in a function declaration?
Give a one-line C expression to test whether a number is a power of 2. [No loops allowed - it's a simple test.]
Can we compile a program without main() function?
How can I determine whether a machines byte order is big-endian or little-endian?
What’s the special use of UNIONS?
What is a null string in c?
Can a variable be both const and volatile?
Explain which function in c can be used to append a string to another string?
What is difference between static and global variable in c?
write a c program to calculate sum of digits till it reduces to a single digit using recursion
Which driver is a pure java driver