I am developing an application in which i want to encode the Spanish text. But the problem is that,it doesn't encode the special characters such as á, é, í, ó, ú, ü,Á, É, Í, Ó, Ú, Ü,Ñ,ñ . How can i do this?i want to encode-decode the spanish text.
Asked
Active
Viewed 1.2k times
2 Answers
4
For international support using simple UTF-8
encoding to encode/decode
your data should be enough.
Utf-8
has a beautiful capability to be able to read ASCII
with one byte, as ordinary ASCII, and Unicode characters with 2 bytes. So it's able "to shrink" when it's necesary.
For complete C#
documentation look on
UTF-8
EDIT
Encoding enc = new UTF8Encoding(true, true);
string value = " á, é, í, ó, ú, ü,Á, É, Í, Ó, Ú, Ü,Ñ,ñ ";
byte[] bytes= enc.GetBytes(value); //convert to BYTE array
//save in some file
//after can read from the file like
string decodedString = enc.GetString(byteArrayReadFromFile);

Tigran
- 61,654
- 8
- 86
- 123
-
i tried utf-8 and base64 but it failed to encode non-ascii characters – dd619 Sep 25 '12 at 06:54
-
I think you don't use it in a right way. Can provide some code ? – Tigran Sep 25 '12 at 07:10
-
//encoding using utf-8 public static string EncodeTo641(string toEncode) { byte[] toEncodeAsBytes= System.Text.UTF8Encoding.UTF8.GetBytes(toEncode); string returnValue= System.Convert.ToBase64String(toEncodeAsBytes); return returnValue; } //decoding strings public static string DecodeFrom64(string encodedData) { byte[] encodedDataAsBytes= System.Convert.FromBase64String(encodedData); string returnValue =System.Text.UTF8Encoding.UTF8.GetString(encodedDataAsBytes); return returnValue; } – dd619 Sep 25 '12 at 07:30
-
@dd619: you have to use manipulation methods present in that class in order to correctly write/read data in that format. Declaring it's not enough. – Tigran Sep 25 '12 at 07:32
-
@Tigren: when i pass " á, é, í, ó, ú, ü,Á, É, Í, Ó, Ú, Ü,Ñ,ñ " as encode it gives á, é, Ã, ó, ú, ü,Ã, É, Ã, Ó, Ú, Ü,Ñ,ñ as output – dd619 Sep 26 '12 at 09:31
-
@dd619: I see you translete the text to 64bit string, why do you need that? – Tigran Sep 26 '12 at 09:35
-
i just tried ur code as string decodedString = enc.GetString(bytes); – dd619 Sep 26 '12 at 11:40
-
but I don't use 64base conversion, why do you. I translate your string into byte array, fter save it into file, and read it from the file to see that all caharcters are the same I saved before. – Tigran Sep 26 '12 at 11:42
0
ok,I am answering my own question ,Hope it will help someone; to print spanish or any other non-ascii character in the given string replace all non-ascii characters by their unicode escape character set E.g repalce á by \u00e1 And then simply print the string.
i.e
string str="árgrgrgrááhhttá";
str=str.Replace("á", "\u00e1");

dd619
- 5,910
- 8
- 35
- 60