Groups | Blog | Home
all groups > dotnet framework > december 2004 >

dotnet framework : International strings/chars


Dobieslaw Wroblewski
12/31/2004 6:16:24 AM
Hello,

Please help me. I think I found this once but cannot find again :-(

Is there any .NET framework class/method that returns the international
counterpart of a given string or character? For example - for 'ó' it returns
'o'.

DW.

Morten Wennevik
12/31/2004 12:23:39 PM
Hi Dobieslaw,

There isn't an easy way to do this, but this hack seem to work for most
characters.

string s = "áàäãâåéèëêíìïîóòöõôøúùüûýÿ";
byte[] b = Encoding.GetEncoding(1251).GetBytes(s);
string t = Encoding.ASCII.GetString(b);

t == aaaaaaeeeeiiiioooooouuuuyy

--
Happy Coding!
Dobieslaw Wroblewski
1/1/2005 12:24:36 PM
[quoted text, click to view]

Thanks, it works!

I believe I used Encoding.ASCII for writing to text files, but in such cases
I got question marks instead of the national characters - so this trick is
quite mysterious for me, but - goodie - it works :-).

DW.

Jon Skeet [C# MVP]
1/1/2005 5:22:02 PM
Dobieslaw Wroblewski=20
[quoted text, click to view]

Yes, you would - ASCII doesn't contain any accented characters.

[quoted text, click to view]

Unfortunately, the above isn't really guaranteed to work. The Encoding=20
class doesn't specify any behaviour for when GetString is presented=20
with bytes which aren't a valid encoded form for that encoding. It may=20
work now, but there's no guarantee it'll work in the future at all.

I suspect you want something like "normalizatino form D" of the Unicode=20
string, followed by a removal of all accents etc - but I don't know=20
enough about normalization to be sure, and I don't know of any=20
implementations of that for .NET :(

If you know that all the characters you care about are within a certain=20
range, I'd suggest a hand-crafted mapping.

--=20
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
Dobieslaw Wroblewski
1/2/2005 2:36:28 AM
[quoted text, click to view]

Well, that always could be done ;-). I just thought of something smarter.
And I think I saw some function for that, but now I cannot remember for sure
if this was .NET, Win32 API or... maybe even Java ;-).

DW.

Jon Skeet [C# MVP]
1/2/2005 7:49:00 AM
Dobieslaw Wroblewski
[quoted text, click to view]

I wouldn't be surprised if there were a call in Win32, and you may be
able to use P/Invoke to use that call. You could try asking in a
Windows internationalization newsgroup...

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
AddThis Social Bookmark Button