How do I output Unicode characters as a pair of ASCII characters?

≯℡__Kan透↙ 提交于 2019-12-24 06:22:55

问题


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.


回答1:


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");



回答2:


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?



来源:https://stackoverflow.com/questions/2633245/how-do-i-output-unicode-characters-as-a-pair-of-ascii-characters

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!