PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.c > what's the different between the %x and %c
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
what's the different between the %x and %c

Réponse
 
LinkBack Outils de la discussion
Vieux 23/11/2007, 09h15   #1
zhangsonglovexiaoniuniu@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut what's the different between the %x and %c

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
  Réponse avec citation
Vieux 23/11/2007, 09h26   #2
touchskyer
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: what's the different between the %x and %c

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


  Réponse avec citation
Vieux 23/11/2007, 09h33   #3
zhangsonglovexiaoniuniu@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: what's the different between the %x and %c

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.
  Réponse avec citation
Vieux 10/12/2007, 04h21   #4
David Thompson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: what's the different between the %x and %c

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
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 04h30.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,12674 seconds with 12 queries