Reputation: 48045
I've a text files, with text such as this:
025069;Zoppè di Cadore;BL;VEN;0437;32010;M189;271;http://www.comuni-italiani.it/025/069/
if I open with Notepad++, it says the encoding is ANSI. So, I'm trying to convert it and save in UTF8:
var ABSfilePath = Server.MapPath(UiUtils.GetPath + "/utility/listacomuni.txt");
var myString = File.ReadAllText(ABSfilePath);
byte[] bytes = Encoding.Default.GetBytes(myString);
myString = Encoding.UTF8.GetString(bytes);
File.WriteAllText(Server.MapPath(UiUtils.GetPath + "/utility/listacomuni2.txt"), myString);
but the result is:
025069;Zopp? di Cadore;BL;VEN;0437;32010;M189;271;http://www.comuni-italiani.it/025/069/
Zoppè
now is Zopp?
, and I've lost a char. Where am I wrong?
Upvotes: 1
Views: 1086
Reputation: 21138
The problems seems to be when reading the file, try this:
var ABSfilePath = Server.MapPath(UiUtils.GetPath + "/utility/listacomuni.txt");
var myBytes = File.ReadAllBytes(ABSfilePath);
var utf8Bytes byte[] = ASCIIEncoding.Convert(ASCIIEncoding.ASCII, UTF8Encoding.UTF8, myBytes);
File.WriteAllBytes(Server.MapPath(UiUtils.GetPath + "/utility/listacomuni2.txt"), utf8Bytes);
When you read the string using File.ReadAllBytes
, it will load it as UTF-8
already. With loading it as Byte-Array, we skip this part.
You also need to use ASCIIEncoding.Convert
for converting the data.
Upvotes: 1