More detail:  For nearly all compilers, int is 32 bits. So I think you
mean "FFFFFFFF" should return a negative number.  I just ran the code,
and it does this as expected.

To stop parsing prior to overflow, say something like

int hex_to_int(char *s)
{
  int i = 0;
  while (*s) {
    if ('0' <= *s && *s <= '9')
      i = (i << 4) + (*s - '0');
    else if ('a' <= *s && *s <= 'f')
      i = (i << 4) + *s + (10 - 'a');
    else if ('A' <= *s && *s <= 'F')
      i = (i << 4) + *s + (10 - 'A');
    else break;
    if (i & 0xf000000) break;
    s++;
  }
  return i;
}


On Jan 15, 10:01 pm, Gene <[email protected]> wrote:
> And it ought to.  Did you try it?
>
> On Jan 15, 9:55 pm, Ashish Goel <[email protected]> wrote:
>
>
>
> > a 0xFFFFFFFFFFFFFFFF should come as a negative number
>
> > Best Regards
> > Ashish Goel
> > "Think positive and find fuel in failure"
> > +919985813081
> > +919966006652
>
> > On Mon, Jan 16, 2012 at 8:19 AM, Gene <[email protected]> wrote:
> > > Okay. Normally you want to use a library that someone else has
> > > tested.  So:
>
> > > sscanf("%x", &i);
>
> > > If you don't have access to any libraries, then:
>
> > > /*
> > >  * Return the integer value of the hex string s. Allowable characters
> > >  * are 0-9, a-f, A-F.  Any other character causes scanning to stop and
> > >  * the hex value of the string up to that character is returned. If
> > > the first
> > >  * character is non-hex, then zero is returned.
> > >  */
> > > int hex_to_int(char *s)
> > > {
> > >  int i = 0;
> > >  while (*s) {
> > >    if ('0' <= *s && *s <= '9')
> > >      i = (i << 4) + (*s - '0');
> > >    else if ('a' <= *s && *s <= 'f')
> > >      i = (i << 4) + *s + (10 - 'a');
> > >    else if ('A' <= *s && *s <= 'F')
> > >      i = (i << 4) + *s + (10 - 'A');
> > >    else break;
> > >    s++;
> > >  }
> > >  return i;
> > > }
>
> > > On Jan 15, 9:16 pm, Ashish Goel <[email protected]> wrote:
> > > > i would assume C or C++
>
> > > > Best Regards
> > > > Ashish Goel
> > > > "Think positive and find fuel in failure"
> > > > +919985813081
> > > > +919966006652
>
> > > > On Mon, Jan 16, 2012 at 7:40 AM, Gene <[email protected]> wrote:
> > > > > Which language?
>
> > > > > On Jan 15, 8:23 pm, Ashish Goel <[email protected]> wrote:
> > > > > > Hi,
>
> > > > > > given a string in hex, convert it into int.
> > > > > > Write test cases for the same.
>
> > > > > > Best Regards
> > > > > > Ashish Goel
> > > > > > "Think positive and find fuel in failure"
> > > > > > +919985813081
> > > > > > +919966006652
>
> > > > > --
> > > > > You received this message because you are subscribed to the Google
> > > Groups
> > > > > "Algorithm Geeks" group.
> > > > > To post to this group, send email to [email protected].
> > > > > To unsubscribe from this group, send email to
> > > > > [email protected].
> > > > > For more options, visit this group at
> > > > >http://groups.google.com/group/algogeeks?hl=en.
>
> > > --
> > > You received this message because you are subscribed to the Google Groups
> > > "Algorithm Geeks" group.
> > > To post to this group, send email to [email protected].
> > > To unsubscribe from this group, send email to
> > > [email protected].
> > > For more options, visit this group at
> > >http://groups.google.com/group/algogeeks?hl=en.

-- 
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.

Reply via email to