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
Explain is it better to bitshift a value than to multiply by 2?
I have a varargs function which accepts a float parameter?
How can I implement sets or arrays of bits?
Write a program on swapping (100, 50)
Explain what is wrong in this statement?
Explain output of printf("Hello World"-'A'+'B'); ?
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?
What is a wrapper function in c?
how should functions be apportioned among source files?
What is modeling?
Is null always equal to 0(zero)?
What is the use of printf() and scanf() functions?
How does selection sort work in c?
Here is a neat trick for checking whether two strings are equal
How do I get an accurate error status return from system on ms-dos?