473,399 Members | 2,159 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,399 software developers and data experts.

How to tell a char is ascii or unicode?

hi,

I want to know if a string contain any unicode character, and what I
did is like this:
for(int i = 0 ; i < str.Length; i++)
{
char ch = str[i];
//Then, right here, how to tell this char is ascii or unicode?
}

thanks.

Apr 10 '06 #1
9 12931
First, you'll have to rephrase the question you're asking your code to
answer.

Unicode is a superset of ASCII, so the question is really, "Is there
any character in this Unicode string that cannot be represented in
ASCII?"

One way to find out is to use an ASCIIEncoding to transform the string
into an array of ASCII bytes, then back into a Unicode string, and
compare the strings. If any character has changed, then that character
couldn't be represented as ASCII.

Apr 10 '06 #2

"Bruce Wood" <br*******@canada.com> wrote in message
news:11*********************@e56g2000cwe.googlegro ups.com...
First, you'll have to rephrase the question you're asking your code to
answer.

Unicode is a superset of ASCII, so the question is really, "Is there
any character in this Unicode string that cannot be represented in
ASCII?"

One way to find out is to use an ASCIIEncoding to transform the string
into an array of ASCII bytes, then back into a Unicode string, and
compare the strings. If any character has changed, then that character
couldn't be represented as ASCII.


An easier way, though, is to check whether its numeric value is > 127.
Apr 10 '06 #3
How about calling ReadXml method without XmlReadMode argument"

dsLog.ReadXml("pi_feedback_log.xml");
Apr 11 '06 #4
Hi,
"yogeshprabhu" <yo**********@discussions.microsoft.com> wrote in message
news:ED**********************************@microsof t.com...
How about calling ReadXml method without XmlReadMode argument"

dsLog.ReadXml("pi_feedback_log.xml");

And how this will tell you about unicode/ascii characters?

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Apr 11 '06 #5
HI,

"Mike Schilling" <ap@newsgroup.nospam> wrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...

"Bruce Wood" <br*******@canada.com> wrote in message
news:11*********************@e56g2000cwe.googlegro ups.com...
First, you'll have to rephrase the question you're asking your code to
answer.

Unicode is a superset of ASCII, so the question is really, "Is there
any character in this Unicode string that cannot be represented in
ASCII?"

One way to find out is to use an ASCIIEncoding to transform the string
into an array of ASCII bytes, then back into a Unicode string, and
compare the strings. If any character has changed, then that character
couldn't be represented as ASCII.


An easier way, though, is to check whether its numeric value is > 127.


I think this is the best way
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Apr 11 '06 #6
Yes. I initially rejected that out of concerns that ASCIIEncoding might
transform some characters above 127 into valid ASCII characters, but
then the transform back would transform these into Unicode characters
below 128, and so cases like this would fail my test, as well.

So, yes, checking whether the numeric value is > 127 is safe, and much
quicker than doing the ASCIIEncoding thing.

Apr 11 '06 #7
My bad, apparently I wanted to reply to two different posts, and they got
intermixed. This was meant for one of the XmlReader question. Thanks for
pointing out.
Apr 11 '06 #8

"Bruce Wood" <br*******@canada.com> wrote in message
news:11**********************@j33g2000cwa.googlegr oups.com...
Yes. I initially rejected that out of concerns that ASCIIEncoding might
transform some characters above 127 into valid ASCII characters, but
then the transform back would transform these into Unicode characters
below 128, and so cases like this would fail my test, as well.

So, yes, checking whether the numeric value is > 127 is safe, and much
quicker than doing the ASCIIEncoding thing.


Just being pedantic here but the subject is misleading - a char is ALWAYS
unicode by definition - the subject should be "How to tell if a char is a
valid ASCII character (as well as unicode)"
Apr 12 '06 #9
thank you guys.
I knew I could see the value if it is greater than 127, but not sure if
it was safe. Now, I think it should be safe.
In, c/c++, there is isascii() function, I remember I saw some similar
function in .net, but just could not remember what it exact is. This is
why I asked.
Thanks again.

Apr 14 '06 #10

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

Similar topics

6
by: Markus Hämmerli | last post by:
I ' ll tra to convert a Cstring to char* without success. I working in a Unicode enabled environment this is working in Unicode CString source = _T("TestString"); TCHAR *szSource =...
4
by: webdev | last post by:
lo all, some of the questions i'll ask below have most certainly been discussed already, i just hope someone's kind enough to answer them again to help me out.. so i started a python 2.3...
6
by: NormD | last post by:
I'm sending a string (xml string) to web service as a parameter. One of the tags in the xml string is the address field and the values of this tag have LF + CR chars. When I receive the string in...
4
by: à°¶à±à°°à±€à°¨à°¿à°µà°¾à°¸ | last post by:
Hai friends, Can any one tell me how can i remove a character from a unocode text. à°•à°²à±â€Œ&హార is a Telugu word in Unicode. Here i want to remove '&' but not replace with a zero width...
11
by: George Sakkis | last post by:
The following snippet results in different outcome for (at least) the last three major releases: # Python 2.3.4 u'%94' # Python 2.4.2 UnicodeDecodeError: 'ascii' codec can't decode byte...
8
by: Frank | last post by:
I have a c program that uses the "A" version of API files. Since it runs on XP I'd guess it would be better if it used the "W" versions. Why is it using the "A" version. I looked in...
1
by: veblen.lee | last post by:
for an example: 'a' value 0x61 '1' value 0x31.
2
by: =?Utf-8?B?U3RldmVu?= | last post by:
Hi, I'm new at Visual C++ and I can't figure out how to change the text property of a label. I'm using a char variable that I'd like to assign to the label. Perhaps I should use a System::String^...
5
by: sniipe | last post by:
Hi, I have a problem with unicode string in Pylons templates(Mako). I will print first char from my string encoded in UTF-8 and urllib.quote(), for example string '£ukasz': ...
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: 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
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...
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
jinu1996
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...
0
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...
0
tracyyun
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.