Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 10 Jun 1997 00:22:20 +0930 (CST)
From:      Michael Smith <msmith@atrad.adelaide.edu.au>
To:        hackers@freebsd.org
Subject:   %i conversion in sscanf?
Message-ID:  <199706091452.AAA29091@genesis.atrad.adelaide.edu.au>

next in thread | raw e-mail | index | archive | help

Hmm.  Beating up on some code here (study break 8) I have an
interesting situation where the %i format is not being interpreted as
the author of the code I'm munging obviously believed it should.

To cut a long story short; from sscanf(3) :

     i     Matches an optionally signed integer; the next pointer must be a
           pointer to int. The integer is read in base 16 if it begins with
           `0x' or `0X', in base 8 if it begins with `0', and in base 10 oth-
           erwise.  Only characters that correspond to the base are used.

Ok.  Fairly ambiguous.  Some test examples :

token: '0x12345678' -> int 0x12345678
token: '0xABCDEF45' -> int 0x7fffffff

Hmm.  Not _necessarily_ expected, although it makes reasonable sense.
However, what I wonder is is this "right" by whatever standard governs
sscanf()?

If not, a question.  Is -0x12345678 a legitimate numeric represenatation?
If so, how about -0x80000000?

It's "fairly" clear that %i might be expected to convert 0xABCDEF45
to a negative number - Tcl thinks that way :

silver:/tmp>tclsh
% expr 0xABCDEF45
-1412567227

So, a judgement call from the numeric gods, perchance?

-- 
]] Mike Smith, Software Engineer        msmith@gsoft.com.au             [[
]] Genesis Software                     genesis@gsoft.com.au            [[
]] High-speed data acquisition and      (GSM mobile)     0411-222-496   [[
]] realtime instrument control.         (ph)          +61-8-8267-3493   [[
]] Unix hardware collector.             "Where are your PEZ?" The Tick  [[



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?199706091452.AAA29091>