what's the different between the %x and %c

  • Thread starter zhangsonglovexiaoniuniu
  • Start date
Z

zhangsonglovexiaoniuniu

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
 
T

touchskyer

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);x=124
scanf("%c", &c);
c='1'


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
 
Z

zhangsonglovexiaoniuniu

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'

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.
 
D

David Thompson

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,830
Latest member
HeleneMull

Latest Threads

Top