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


Please Help Members By Posting Answers For Below Questions

Explain is it better to bitshift a value than to multiply by 2?

724


I have a varargs function which accepts a float parameter?

590


How can I implement sets or arrays of bits?

613


Write a program on swapping (100, 50)

649


Explain what is wrong in this statement?

645






Explain output of printf("Hello World"-'A'+'B'); ?

985


A banker has a seif with a cipher. Not to forget the cipher, he wants to write it coded as following: each digit to be replaced with the difference of 9 with the current digit. The banker chose a cipher. Decipher it knowing the cipher starts with a digit different than 9. I need to write a program that takes the cipher from the keyboard and prints the new cipher. I thought of the following: Take the input from the keyboard and put it into a string or an array. Go through the object with a for and for each digit other than the first, substract it from 9 and add it to another variable. Print the new variable. Theoretically I thought of it but I don't know much C. Could you give me any kind of hint, whether I am on the right track or not?

1520


What is a wrapper function in c?

600


how should functions be apportioned among source files?

634


What is modeling?

655


Is null always equal to 0(zero)?

602


What is the use of printf() and scanf() functions?

642


How does selection sort work in c?

635


Here is a neat trick for checking whether two strings are equal

574


How do I get an accurate error status return from system on ms-dos?

657