Convert Unicode to Windows-1252 for vCards

I am trying to write a program in C # that will split a vCard (VCF) file with multiple contacts into separate files for each contact. I understand that vCard needs to be saved as ANSI (1252) for most mobile phones in order to read them.

However, if I open the VCF file with StreamReader and then write it with StreamWriter (setting 1252 as the encoding format), all special characters like Γ₯ , Γ¦ and ΓΈ will get written as ? . Of course, ANSI (1252) will support these characters. How to fix it?

Edit: Here is a piece of code that I use to read and write a file.

 private void ReadFile() { StreamReader sreader = new StreamReader(sourceVCFFile); string fullFileContents = sreader.ReadToEnd(); } private void WriteFile() { StreamWriter swriter = new StreamWriter(sourceVCFFile, false, Encoding.GetEncoding(1252)); swriter.Write(fullFileContents); } 
+6
c # unicode character-encoding windows-1252
source share
1 answer

You think that Windows-1252 supports the special characters listed above (see the Wikipedia entry for the full list).

 using (var writer = new StreamWriter(destination, true, Encoding.GetEncoding(1252))) { writer.WriteLine(source); } 

In my test application using the above code, this result was obtained:

Look at the cool letters I can make: Γ₯, Γ¦, and ΓΈ!

No question marks found. Do you set the encoding when you read it using StreamReader ?

EDIT: You should simply use Encoding.Convert to convert the VCF UTF-8 file to Windows-1252. No need for Regex.Replace . Here's how I do it:

 // You might want to think of a better method name. public string ConvertUTF8ToWin1252(string source) { Encoding utf8 = new UTF8Encoding(); Encoding win1252 = Encoding.GetEncoding(1252); byte[] input = source.ToUTF8ByteArray(); // Note the use of my extension method byte[] output = Encoding.Convert(utf8, win1252, input); return win1252.GetString(output); } 

And here is what my extension method looks like:

 public static class StringHelper { // It should be noted that this method is expecting UTF-8 input only, // so you probably should give it a more fitting name. public static byte[] ToUTF8ByteArray(this string str) { Encoding encoding = new UTF8Encoding(); return encoding.GetBytes(str); } } 

Also, you probably want to add using to your ReadFile and WriteFile methods.

+12
source share

All Articles