Hi, everyone. The other day I wrote a piece of code to see how the
UTF-8 text file was saved in the memory. Although the code worked
seemingly well, I still cannot understand a problem about the output
format.
The code is like this (I ignored the #include and #define here) :
int main(void)
{
FILE* fp = NULL;
char buffer[MAX_SIZE];
int byteRead = 0;
char* cp = NULL;
memset(buffer, '\0', sizeof(buffer));
if ((fp = fopen(FILE_NAME, "r")) != NULL)
{
byteRead = fread(buffer, sizeof(char), MAX_SIZE, fp);
if (byteRead != 0)
{
cp = (char *)buffer;
while ((*cp) != '\0')
{
printf("%#X\t", *cp++);
}
}
else
{
}
fclose(fp);
}
else
{
perror("open");
}
return 0;
}
The UTF-8 text file it reads in contains five Japanese characters
("あいうえお") and four ascii character ("abcd"). And the output
is as below:
0XFFFFFFEF 0XFFFFFFBB 0XFFFFFFBF 0XFFFFFFE3
0XFFFFFF81
0XFFFFFF82 0XFFFFFFE3 0XFFFFFF81 0XFFFFFF84
0XFFFFFFE3
0XFFFFFF81 0XFFFFFF86 0XFFFFFFE3 0XFFFFFF81
0XFFFFFF88
0XFFFFFFE3 0XFFFFFF81 0XFFFFFF8A 0X61 0X62 0X63
0X64
Well,The result is fine, but I don't understand how do those "FFFFFF"
come out? In my opinion, printf("%#X\t", *cp++) will only output the
content of one byte, but the output seems like four bytes. I don't know
why. Can anybody tell my how does it happen? Thanx~~~ 8 2705
On 17 Mar 2006 21:58:23 -0800, "Claude Yih" <wi******@gmail.com>
wrote: Hi, everyone. The other day I wrote a piece of code to see how the UTF-8 text file was saved in the memory. Although the code worked seemingly well, I still cannot understand a problem about the output format.
The code is like this (I ignored the #include and #define here) :
int main(void) { FILE* fp = NULL; char buffer[MAX_SIZE]; int byteRead = 0; char* cp = NULL;
memset(buffer, '\0', sizeof(buffer));
if ((fp = fopen(FILE_NAME, "r")) != NULL) { byteRead = fread(buffer, sizeof(char), MAX_SIZE, fp); if (byteRead != 0) { cp = (char *)buffer;
The cast is superfluous.
while ((*cp) != '\0') { printf("%#X\t", *cp++);
%x requires an unsigned int. On your system, char is apparently
signed. Try changing cp to an unsigned char* and buffer to an
unsigned char array.
} } else { }
If you don't have any else action, you don't need the else at all.
fclose(fp); } else { perror("open"); } return 0; }
The UTF-8 text file it reads in contains five Japanese characters ("?????") and four ascii character ("abcd"). And the output is as below:
0XFFFFFFEF 0XFFFFFFBB 0XFFFFFFBF 0XFFFFFFE3 0XFFFFFF81 0XFFFFFF82 0XFFFFFFE3 0XFFFFFF81 0XFFFFFF84 0XFFFFFFE3 0XFFFFFF81 0XFFFFFF86 0XFFFFFFE3 0XFFFFFF81 0XFFFFFF88 0XFFFFFFE3 0XFFFFFF81 0XFFFFFF8A 0X61 0X62 0X63 0X64
Well?The result is fine, but I don't understand how do those "FFFFFF" come out? In my opinion, printf("%#X\t", *cp++) will only output the content of one byte, but the output seems like four bytes. I don't know why. Can anybody tell my how does it happen? Thanx~~~
Since printf is a variadic function, all integer arguments smaller
than int (unsigned int) are promoted to int (or unsigned int). Think
about why 0x81 has the Fs but 0x61 doesn't.
Similarly, all floats would be promoted to double.
Remove del for email
"Claude Yih" <wi******@gmail.com> writes: Hi, everyone. The other day I wrote a piece of code to see how the UTF-8 text file was saved in the memory. Although the code worked seemingly well, I still cannot understand a problem about the output format.
The code is like this (I ignored the #include and #define here) :
int main(void) {
[snip]
Why would you want to ignore the #include and #define directives?
Your program is invalid without them. If you're going to post a
program, post the whole thing (unless it's too big to post).
--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Claude Yih wrote: Hi, everyone. The other day I wrote a piece of code to see how the UTF-8 text file was saved in the memory. Although the code worked seemingly well, I still cannot understand a problem about the output format.
The code is like this (I ignored the #include and #define here) :
I won't ignore #includes and #defines. It is expected that code posted
here be compilable. The remainder of Claude Yih's question follows this
note.
See what the following does on your implementation. Note the 'unsigned'.
#include <stdio.h>
#define FILE_NAME "./japchar"
int main(void)
{
FILE *fp = NULL;
unsigned char buffer[BUFSIZ], *cp;
if ((fp = fopen(FILE_NAME, "rb"))) {
if (fread(buffer, 1, BUFSIZ, fp))
for (cp = buffer; *cp; cp++)
printf("%#X\t", *cp);
putchar('\n');
fclose(fp);
}
else
perror("open");
return 0;
}
[output for my implementation with ./japchar containing the characters
あいうえお + EOL characters]
0XE3 0X81 0X82 0XE3 0X81 0X84 0XE3 0X81 0X86
0XE3 0X81 0X88 0XE3 0X81 0X8A 0XD 0XA
int main(void) { FILE* fp = NULL; char buffer[MAX_SIZE]; int byteRead = 0; char* cp = NULL;
memset(buffer, '\0', sizeof(buffer));
if ((fp = fopen(FILE_NAME, "r")) != NULL) { byteRead = fread(buffer, sizeof(char), MAX_SIZE, fp); if (byteRead != 0) { cp = (char *)buffer; while ((*cp) != '\0') { printf("%#X\t", *cp++); } } else { } fclose(fp); } else { perror("open"); } return 0; }
The UTF-8 text file it reads in contains five Japanese characters ("あいうえお") and four ascii character ("abcd"). And the output is as below:
0XFFFFFFEF 0XFFFFFFBB 0XFFFFFFBF 0XFFFFFFE3 0XFFFFFF81 0XFFFFFF82 0XFFFFFFE3 0XFFFFFF81 0XFFFFFF84 0XFFFFFFE3 0XFFFFFF81 0XFFFFFF86 0XFFFFFFE3 0XFFFFFF81 0XFFFFFF88 0XFFFFFFE3 0XFFFFFF81 0XFFFFFF8A 0X61 0X62 0X63 0X64
Well,The result is fine, but I don't understand how do those "FFFFFF" come out? In my opinion, printf("%#X\t", *cp++) will only output the content of one byte, but the output seems like four bytes. I don't know why. Can anybody tell my how does it happen? Thanx~~~
Yeh, referring to the <Computer Systems - A Programmer's Perspective>,
I knew why I should change the char into unsigned char here. Thanx,
buddies :)
P.S: Next time, I won't ignore #include and #define any more :) Thanks
for reminding me of that.
Claude Yih opined: Yeh, referring to the <Computer Systems - A Programmer's Perspective>, I knew why I should change the char into unsigned char here. Thanx, buddies :)
But we don't, as you didn't post any context. Read:
<http://cfaj.freeshell.org/google/>
<http://clc-wiki.net/wiki/Introduction_to_comp.lang.c>
P.S: Next time, I won't ignore #include and #define any more :) Thanks for reminding me of that.
Also, don't forget to follow good advice at the above URLs.
--
BR, Vladimir
For adult education nothing beats children. <http://cfaj.freeshell.org/google/> <http://clc-wiki.net/wiki/Introduction_to_comp.lang.c>
Also, don't forget to follow good advice at the above URLs.
I'd admit those URLs helped a lot. I'll pay attention to that when I
post another message next time.
Claude Yih opined: <http://cfaj.freeshell.org/google/> <http://clc-wiki.net/wiki/Introduction_to_comp.lang.c>
Also, don't forget to follow good advice at the above URLs.
I'd admit those URLs helped a lot. I'll pay attention to that when I post another message next time.
Thank you!
--
BR, Vladimir
Your business will go through a period of considerable expansion. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: eScrewDotCom |
last post by:
eScrew
Welcome to eScrew!
eScrew is eScrew and this is eScrew story. eScrew will tell you eScrew
story if you promise eScrew to consider eScrew story as joke. eScrew
story is very funny. eScrew...
|
by: eScrewDotCom |
last post by:
eScrew
Welcome to eScrew!
eScrew is eScrew and this is eScrew story. eScrew will tell you eScrew
story if you promise eScrew to consider eScrew story as joke. eScrew
story is very funny. eScrew...
|
by: Haines Brown |
last post by:
I'm having trouble finding the character entity for the French
abbreviation for "number" (capital N followed by a small supercript
o, period).
My references are not listing it. Where would I...
|
by: eScrewDotCom |
last post by:
www.eScrew.com
eScrew
Welcome to eScrew!
eScrew is eScrew and this is eScrew story. eScrew will tell you eScrew
story if you promise eScrew to consider eScrew story as joke. eScrew
story is...
|
by: Pavils Jurjans |
last post by:
Hello,
Here's an excerpt from msdn online documentation:
An index is the position of a Char, not a Unicode character, in a String. An
index is a zero-based, nonnegative number starting from...
| |
by: Steven T. Hatton |
last post by:
This is one of the first obstacles I encountered when getting started with
C++. I found that everybody had their own idea of what a string is. There
was std::string, QString, xercesc::XMLString,...
|
by: =?GB2312?B?1qP2zrrA?= |
last post by:
hi all,
i need to covert the utf8 character to gbk,is that possible?Any idea
will be appreciate.
|
by: Elliot |
last post by:
My XML is using encoding UTF-8 and its content contains Chinese character.
When debug the following codes:
string strXmlFile = "xml.xml";
XmlDocument objXml = new XmlDocument();
...
|
by: raphfrk |
last post by:
Is this valid?
int a;
void *b;
b = (void *)a; // b points to a
b += 5*sizeof(*a); // b points to a
a = 100;
|
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,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
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 projectplanning, coding, testing,...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |