Write code for atoi(x) where x is hexadecimal string.

Answers were Sorted based on User's Feedback



Write code for atoi(x) where x is hexadecimal string...

Answer / yuusaku

int num;
sscanf(string,"%x",&num);

Is This Answer Correct ?    10 Yes 1 No

Write code for atoi(x) where x is hexadecimal string...

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

Write code for atoi(x) where x is hexadecimal string...

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

Write code for atoi(x) where x is hexadecimal string...

Answer / vadivel t

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

Write code for atoi(x) where x is hexadecimal string...

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

Post New Answer

More C Interview Questions

sir i got 146 correct question & i have also the advantage of height so will they consider my marks as 146+3=149.can any body tell me how they consider my height marks.

1 Answers  


What is the difference between macros and inline functions?

5 Answers   Global Edge, L&T,


How the c program is executed?

0 Answers  


What is a program?

0 Answers  


Why does this code crash?

0 Answers  






What are the __date__ and __time__ preprocessor commands?

0 Answers  


What is the difference between constant pointer and pointer to a constant. Give examples.

4 Answers   TCS,


What does %p mean?

0 Answers  


though sbi was nationalized why its not comes under nationalized banks and its comes under publicsector banks

3 Answers   State Bank Of India SBI,


write a program whose output will be- 1 12 123 1234

10 Answers  


What is structure and union in c?

0 Answers  


WHOT IS CHAR?

4 Answers   TCS,


Categories