tags:

views:

129

answers:

2

How do I convert (as an example):

Señor Coconut Y Su Conjunto - Introducciõn

to:

Señor Coconut Y Su Conjunto - Introducciõn

I've got an app that creates m3u playlists, but when the track filename, artist or title contains non ASCII characters it doesn't get read properly by the music player so the track doesn't get played.

I've discovered that if I write the track out as:

#EXTINFUTF8:76,Señor Coconut Y Su Conjunto - Introducciõn
#EXTINF:76,Señor Coconut Y Su Conjunto - Introducciõn
#UTF8:01-Introducciõn.mp3
01-Introducciõn.mp3

Then the music player will read it correctly and play the track.

My problem is that I can't find the information I need to be able to do the conversion properly.

I've tried the following:

    byte[] byteArray = Encoding.UTF8.GetBytes(output);
    foreach (Byte b in byteArray)
    {
        playList.Write(b);
    }

where playList = new StreamWriter(filename, false); but I just get a series of numbers output:

#EXTINFUTF8:76,83101195177111114326711199111110117116328932831173267111110106117110116111 - 731101161141111001179999105195181110

which I guess are the numerical values of the characters rather than the characters themselves.

It's been a while since I've done this low level character manipulation and I'm a little rusty.

UPDATE

I've now got:

    byte[] byteArray = Encoding.UTF8.GetBytes(output);
    foreach (Byte b in byteArray)
    {
        playList.Write(Convert.ToChar(b));
    }

to do the output and at first glance it appeared to be working. The file as seen in Notepad++ is showing the correct information. However, the first track still isn't being played.

A: 

well, try to write the encoding player expects. and it is utf8! (i guess)

byte[] bytesToWrite = Encoding.Utf8.GetBytes(yourString);

see that: #UTF8 in your sample?

Andrey
I should have added that I've tried that, but just get a series of numbers.
ChrisF
then you output bytes to file incorrectly. i bet (10 bucks :) ) that it IS utf8. just output it correctly
Andrey
use StreamWriter and pass Encoding.Utf8 to ctor. it will do the trick
Andrey
create StreamWriter with this ctor http://msdn.microsoft.com/library/f5f5x7kt.aspx then call WriteLine(output) or Write(output). in this case you don't need to call GetBytes
Andrey
That just outputs exactly the same character string "Señor" not "Señor" (for example).
ChrisF
how do you check that? using notepad? open file with hex editor or smart notepad++. Señor is same as Señor depending on encoding you open it with.
Andrey
or when using notepad click Open and pick ASCII there
Andrey
I've got an m3u file generated by MediaMonkey and I'm checking it in Notepad++. It displays the same in Notepad too. I know "Señor" not "Señor" are the same, but I need to have the file with no high ASCII characters (as we used to call them) for MM to understand it.
ChrisF
Try Encoding.UTF7. That should keep the high bit out of it.
Rob Parker
click in notepad++, click Encoding, Character Set, Western European, ISO 8859-1 then copy-paste Señor there. then click Encoding, Encode in UTF-8 and witness how Señor turns into Señor. you need just to output your file in C# in UTF8. I gave you the link to msdn.
Andrey
+2  A: 

You want the whole stream to be in UTF-8. Try:

StreamWriter playList = new StreamWriter(filename, false, System.Text.Encoding.UTF8);

Now, to write to the stream, just pass your String named output like this:

playList.Write(output);

The stream will now all be in the proper encoding, so you should also just be able to do something like:

playList.WriteLine("#EXTINFUTF8:76,Señor Coconut Y Su Conjunto - Introducciõn");
lavinio
I think you're right. I'm just checking this now.
ChrisF
ChrisF