>I don't think that an error needs to be thrown, as it will just overflow 
>and wrap around. My concern is that using %c may or may not throw a sign 
>bit into the encoded value. That requires the recipient of that value to 
>know whether the value was negative ahead of time.

No, but he needs to know whether to interpret the MSB is a sign bit or
not (or expressed differently, whether to sign-extend or zero-extend
the value after decoding it).  But he also need to know how many
octets to decode, how is this any different?  %1c can be used to
encode either the range 0..255 or the range -128..127, and you need to
decide which one to use when you specify the binary format you are
encoding into, just like you need to decide whether to use 1, 2 or 17
octets to encode the number.  If you use a number outside of the
selected range, sscanf will truncate it.  If you choose the range
0..255 (%1c), 270 will be truncated to 14 and -3 will be truncated to
253.  If you choose the range -128..127 (%1c for encode, and %+1c for
decode), 130 will be truncated to -126 and -200 to 56.  sprintf will
not check that the value is in the chosen range, that's up to you.

sprintf %Nc will never "throw a sign bit in", it will simply encode
the N*8 least significant bits of the 2-complement binary
representation of the integer.  This is true whether or not the number
is negative.
  • pos... H. William Welliver III
    • ... Marcus Comstedt (ACROSS) (Hail Ilpalazzo!) @ Pike (-) developers forum
      • ... Bill Welliver
        • ... Marcus Comstedt (ACROSS) (Hail Ilpalazzo!) @ Pike (-) developers forum
          • ... Bill Welliver
            • ... Mirar @ Pike developers forum
              • ... Bill Welliver
                • ... Mirar @ Pike developers forum
                • ... Marcus Comstedt (ACROSS) (Hail Ilpalazzo!) @ Pike (-) developers forum
                • ... Marcus Comstedt (ACROSS) (Hail Ilpalazzo!) @ Pike (-) developers forum
        • ... Mirar @ Pike developers forum
          • ... Bill Welliver
            • ... Henrik Grubbstr�m (Lysator) @ Pike (-) developers forum
              • ... Bill Welliver
                • ... Marcus Comstedt (ACROSS) (Hail Ilpalazzo!) @ Pike (-) developers forum
                • ... Mirar @ Pike developers forum
                • ... Marcus Comstedt (ACROSS) (Hail Ilpalazzo!) @ Pike (-) developers forum
    • ... Mirar @ Pike developers forum

Reply via email to