Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   what's the different between the %x and %c (http://www.velocityreviews.com/forums/t554498-whats-the-different-between-the-x-and-c.html)

zhangsonglovexiaoniuniu@gmail.com 11-23-2007 09:15 AM

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

touchskyer 11-23-2007 09:26 AM

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



zhangsonglovexiaoniuniu@gmail.com 11-23-2007 09:33 AM

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.

David Thompson 12-10-2007 04:21 AM

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


All times are GMT. The time now is 04:15 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.