473,320 Members | 2,111 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,320 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 12925
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 Hmmerli | 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: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.