Forum: PC-Programmierung HEX-Zahlen mit scanf einlesen


von Da Mo (Gast)


Lesenswert?

Warum kann ich mit folgendem "Programm", nicht zwei HEX-Zahlen einlesen?

#include <stdio.h>

int main(void)
{
  unsigned char a, b;

  printf("Erste HEX-Zahl(max. FF): 0x");
  scanf("%x", &a);
  printf("Zweite HEX-Zahl(max. FF): 0x");
  scanf("%x", &b);

  printf("%x, %x",a ,b);

  system("PAUSE");
  return 0;
}

Wenn ich die beiden scanf-Anweisungen vertausche, dann geht es!? Muss 
ich das einfach akzeptieren?

Grüße
Austrianer

von Klaus W. (mfgkw)


Lesenswert?

Mit %x wird eine unsigned int gelesen, du hast aber unsigned char.

Dominik M. schrieb:
> Wenn ich die beiden scanf-Anweisungen vertausche, dann geht es!?

Zufall?
a und b mögen stimmen - du überschreibst dafür ein anderes Byte, und 
merkst es nur nicht.

> Muss ich das einfach akzeptieren?

Musst du nicht.
Man muss nur akzeptieren, daß ein falsches Programm nicht richtig läuft. 
:-)

von Da Mo (Gast)


Lesenswert?

Kann man keine HEX-Zahl einlesen?

von Klaus W. (mfgkw)


Lesenswert?

Doch, natürlich.
Aber nach ANSI-Standard nicht in eine char, sondern in eine unsigned int 
(%x), unsigned long (%lx), unsigned short (%hx).

Beim gcc bzw. bei der glibc und m.W. auch nach C99 geht eine unsigned 
char mit %hhx.

Siehe man 3 printf.

von Klaus W. (mfgkw)


Lesenswert?

1
...
2
CONFORMING TO
3
       The  fprintf(), printf(), sprintf(), vprintf(), vfprintf(),
4
       and vsprintf() func‐
5
       tions conform to C89 and C99.  The snprintf() and vsnprintf()
6
       functions  conform
7
       to C99.
8
9
       Concerning  the return value of snprintf(), SUSv2 and C99
10
       contradict each other:
11
       when snprintf() is called with  size=0  then  SUSv2
12
       stipulates  an  unspecified
13
       return  value  less  than  1,  while C99 allows str to be
14
       NULL in this case, and
15
       gives the return value (as always) as the number of
16
       characters that  would  have
17
       been written in case the output string has been large enough.
18
19
       Linux  libc4  knows  about the five C standard flags.
20
       It knows about the length
21
       modifiers h, l, L, and the conversions c, d, e, E, f, F, g,
22
       G, i, n, o, p, s, u,
23
       x,  and  X, where F is a synonym for f.  Additionally, it
24
       accepts D, O, and U as
25
       synonyms for ld, lo, and lu.  (This is bad, and caused
26
       serious bugs later,  when
27
       support for %D disappeared.)  No locale-dependent radix
28
       character, no thousands’
29
       separator, no NaN or infinity, no "%m$" and "*m$".
30
31
       Linux libc5 knows about the five C standard flags and the
32
       ' flag, locale,  "%m$"
33
       and  "*m$".  It knows about the length modifiers h, l, L,
34
       Z, iand q, but accepts
35
       L and q both for long double and for long long int
36
       (this  is  a  bug).   It  no
37
       longer  recognizes  F,  D,  O, and U, but adds the
38
       conversion character m, which
39
       outputs strerror(errno).
40
41
       glibc 2.0 adds conversion characters C and S.
42
43
       glibc 2.1 adds length modifiers hh, j, t, and  z  and
44
       conversion  characters  a
45
       andA.
46
47
       glibc 2.2 adds the conversion character F with C99
48
       semantics, and the flag char‐
49
       acter I.

Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.