473,396 Members | 1,918 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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
Nov 23 '07 #1
3 1773
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.comwrote:
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
Nov 23 '07 #2
On Nov 23, 5:26 pm, touchskyer <xingji...@gmail.comwrote:
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.comwrote:
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.
Nov 23 '07 #3
On Fri, 23 Nov 2007 01:15:33 -0800 (PST),
"zh*********************@gmail.com"
<zh*********************@gmail.comwrote:
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
Dec 10 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

220
by: Brandon J. Van Every | last post by:
What's better about Ruby than Python? I'm sure there's something. What is it? This is not a troll. I'm language shopping and I want people's answers. I don't know beans about Ruby or have...
54
by: Brandon J. Van Every | last post by:
I'm realizing I didn't frame my question well. What's ***TOTALLY COMPELLING*** about Ruby over Python? What makes you jump up in your chair and scream "Wow! Ruby has *that*? That is SO...
7
by: Jonathan Fine | last post by:
Giudo has suggested adding optional static typing to Python. (I hope suggested is the correct word.) http://www.artima.com/weblogs/viewpost.jsp?thread=85551 An example of the syntax he proposes...
125
by: Sarah Tanembaum | last post by:
Beside its an opensource and supported by community, what's the fundamental differences between PostgreSQL and those high-price commercial database (and some are bloated such as Oracle) from...
121
by: typingcat | last post by:
First of all, I'm an Asian and I need to input Japanese, Korean and so on. I've tried many PHP IDEs today, but almost non of them supported Unicode (UTF-8) file. I've found that the only Unicode...
100
by: E. Robert Tisdale | last post by:
What is an object? Where did this term come from? Does it have any relation to the objects in "object oriented programming"?
51
by: jacob navia | last post by:
I would like to add at the beginning of the C tutorial I am writing a short blurb about what "types" are. I came up with the following text. Please can you comment? Did I miss something? Is...
21
by: Helge Jensen | last post by:
I've got some data that has Set structure, that is membership, insert and delete is fast (O(1), hashing). I can't find a System.Collections interface that matches the operations naturally offered...
19
by: Charles Law | last post by:
Take a solution with a project hierarchy along the lines of an n-tier system, so that we have a data layer, business layer and presentation layer. The presentation layer is coupled to the business...
669
by: Xah Lee | last post by:
in March, i posted a essay 鈥淲hat is Expressiveness in a Computer Language鈥, archived at: http://xahlee.org/perl-python/what_is_expresiveness.html I was informed then that there is a academic...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project梡lanning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.