|
|
|
|
||||||
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
Hi all,
i got a program as follows: void str2mac(void) { unsigned char xx[6]; char str[20] = "11:22:33:44:55:66"; /* load str to xx */ 1. sscanf(str,"%x:%x:%x:%x:%x: %x",&xx[0],&xx[1],&xx[2],&xx[3],&xx[4],&xx[5]); 2. sscanf(str,"%c:%c:%c:%c:%c: %c",&xx[0],&xx[1],&xx[2],&xx[3],&xx[4],&xx[5]); } is there some difference between 1 and 2? thanks. Evan |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
why not run it in your local machine? And you will see the difference.
%c is for character %x is for hex scanf("%x", &x); >>124 x=124 scanf("%c", &c); >>124 c='1' On 11ÔÂ23ÈÕ, ÏÂÎç5ʱ15·Ö, "zhangsonglovexiaoniu...@gmail.com" <zhangsonglovexiaoniu...@gmail.com> wrote: > Hi all, > > i got a program as follows: > > void str2mac(void) > { > unsigned char xx[6]; > char str[20] = "11:22:33:44:55:66"; > > /* load str to xx */ > 1. sscanf(str,"%x:%x:%x:%x:%x: > %x",&xx[0],&xx[1],&xx[2],&xx[3],&xx[4],&xx[5]); > > 2. sscanf(str,"%c:%c:%c:%c:%c: > %c",&xx[0],&xx[1],&xx[2],&xx[3],&xx[4],&xx[5]); > > } > > is there some difference between 1 and 2? > > thanks. > Evan |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
On Nov 23, 5:26 pm, touchskyer <xingji...@gmail.com> wrote:
> why not run it in your local machine? And you will see the difference. > > %c is for character > %x is for hex > > scanf("%x", &x);>>124 > > x=124 > scanf("%c", &c);>>124 > > c='1' > > On 11ÔÂ23ÈÕ, ÏÂÎç5ʱ15·Ö, "zhangsonglovexiaoniu...@gmail.com" > > > > <zhangsonglovexiaoniu...@gmail.com> wrote: > > Hi all, > > > i got a program as follows: > > > void str2mac(void) > > { > > unsigned char xx[6]; > > char str[20] = "11:22:33:44:55:66"; > > > /* load str to xx */ > > 1. sscanf(str,"%x:%x:%x:%x:%x: > > %x",&xx[0],&xx[1],&xx[2],&xx[3],&xx[4],&xx[5]); > > > 2. sscanf(str,"%c:%c:%c:%c:%c: > > %c",&xx[0],&xx[1],&xx[2],&xx[3],&xx[4],&xx[5]); > > > } > > > is there some difference between 1 and 2? > > > thanks. > > Evan- Hide quoted text - > > - Show quoted text - sorry , i suddenly mind open. i know where i think wrong. thanks for the god and you. |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
On Fri, 23 Nov 2007 01:15:33 -0800 (PST),
"zhangsonglovexiaoniuniu@gmail.com" <zhangsonglovexiaoniuniu@gmail.com> wrote: > unsigned char xx[6]; > char str[20] = "11:22:33:44:55:66"; > > /* load str to xx */ > 1. sscanf(str,"%x:%x:%x:%x:%x: > %x",&xx[0],&xx[1],&xx[2],&xx[3],&xx[4],&xx[5]); > Doesn't work reliably; %x expects an unsigned _int_ to store into, not a u-char. You might appear to luck out if your (current) machine and C implementation supports little-endian unaligned (u-)ints, and xx happens to be followed by something whose being clobbered doesn't (detectably) cause trouble; the former is true on a certain widely used architecture, and the latter is not too unlikely. In C99 (or with at least this C99-compatible extension to C90) you can use %hhx. Otherwise you need to store into at least u-shorts, and most convenient u-ints, and then copy elementwise. > 2. sscanf(str,"%c:%c:%c:%c:%c: > %c",&xx[0],&xx[1],&xx[2],&xx[3],&xx[4],&xx[5]); Doesn't work at all; (at best) it takes only the first character of the first piece, and then looks for the first colon and (usually) fails. Formally %c expects (pointer to) _plain_ char not unsigned char, but only unrealistically picky implementations care about this. Especially not implementations where plain char 'tastes' unsigned, as most (but not all) do nowadays. - formerly david.thompson1 || achar(64) || worldnet.att.net |
|
![]() |
| Outils de la discussion | |
|
|