Write code for atoi(x) where x is hexadecimal string.
Answers were Sorted based on User's Feedback
Answer / 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 |
Answer / 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 |
Hi,
Refer below link to know how atoi() lib fuction works.
http://www.cppreference.com/wiki/c/string/atoi
And find the equalent code which i have written here.
#include<stdio.h>
#include<conio.h>
int MyAtoi(char *cptr);
main()
{
/*Give different inputs like "12.3432", "a4523"," 123"
"abcd", "1234f" and find the qualent output*/
char *cptr = "123445";
printf("INTEGER EQU IS: %d\n", MyAtoi(cptr));
getch();
}
int MyAtoi(char *cptr)
{
int iptr = 0;
while((*cptr != '\0') && ((*cptr >= 48 && *cptr <= 57) ||
(*cptr == 32)))
{
if(*cptr != ' ')
iptr = (iptr * 10) + (*cptr - 48);
cptr++;
}
return iptr;
}
Is This Answer Correct ? | 3 Yes | 0 No |
Answer / audrius
long l = strtol(string, NULL, 16);
//or, if you need unsigned (which you usually do with hexes)
unsigned long ul = strtoul(string, NULL, 16);
Is This Answer Correct ? | 1 Yes | 0 No |
When the macros gets expanded?
What does the function toupper() do?
Why is malloc used?
Write a client and server program in C language using UDP, where client program interact with the Server as given below: i) The client begins by sending a request to send a string of 8 characters or series of 7 numbers, the server sends back a characters or numbers as per the request of the client. ii) In case of series of 7 numbers: The client sends a multiplication of numbers, to the server. iii) In case of a string of 8 characters: The client sends a reverse order of string to the server.. iv) Server will send an acknowledgment to the client after receiving the correct answer
Explain how can I read and write comma-delimited text?
What is #line?
Why should I prototype a function?
write a program to delete an item from a particular location of an linear array?
How do we make a global variable accessible across files? Explain the extern keyword?
What is wild pointer in c with example?
dennis ritchie invented C language in AT&T bell laboratory what is the extension of AT&T?
Function shall sum members of given one-dimensional array. However, it should sum only members whose number of ones in the binary representation is higher than defined threshold (e.g. if the threshold is 4, number 255 will be counted and 15 will not) - The array length is arbitrary - output the results to the stdout