Using AS2 to solve Chinese ID3 garbled characters

Source: Internet
Author: User
Tags id3 id3 tag
Solve | Chinese

Flash if the MP3 ID3 tag uses GB2312 encoding, then the Flash script output is garbled
Code 1

var s:sound=new Sound (this);
S.loadsound ("Dxh.mp3", false);
S.onid3=function () {
Trace (This.id3.songname);
}

The output results are:
¶¡ïã»

Dxh.mp3 's ID3v1 label correctly should be Songname= " lilac ", it appears that flash on the transfer code on the problem. Let's take a look at songname what is the bottom of this string?

Code 2:var s:sound=new Sound (this);
S.loadsound ("Dxh.mp3", false);
S.onid3=function () {
var songname:string=this.id3.songname;
for (Var i=0;i<songname.length;i++) {
Trace (Songname.charcodeat (i));
}
}
The output results are:
182
161
207
227
187
168
We use calculator to convert into 16 is "B6 A1 CF E3 BB A8";
Just the "Lilac" GB2312 code, we still use flash to try

System.usecodepage=true;
Trace (unescape ("%b6%a1%cf%e3%bb%a8"));
The output results are:
Ding fragrant flowers

Then why code 1 garbled phenomenon, because Flash will GB2312 as a UTF-8 to explain, we again to test:

Code 3:

var s:sound=new Sound (this);
S.loadsound ("Dxh.mp3", false);
S.onid3=function () {
var songname:string=this.id3.songname;
Trace (Escape (songname));
}
The result:
%3f%3f%3f%3f%3f%a1%a7
The cause of the problem we found, as long as the GB2312 converted to UTF-8 code can be shown normal, but if the conversion, we look at code 2, I test the idea

Code 4:
System.usecodepage=true;
var gb:string=unescape ("%b6%a1%cf%e3%bb%a8");
System.usecodepage=false;
Trace (GB);
Trace (Escape (GB));
Output results:
Ding fragrant flowers
%e4%b8%81%e9%a6%99%e8%8a%b1

The second line is the "Lilac" UTF-8 code, that has been converted successfully, we have to specifically implement this process

Class Lm.utils.LUTF {
Public Function Toutf (source:string): string{
var target:string= "";
for (Var i=0;i<source.length;i++) {
Target+=this.codetohex (Source.charcodeat (i));
}
System.usecodepage=true;
Target=unescape (target);
System.usecodepage=false;
return target;
}
Private Function Codetohex (code:number): string{
var low:number=code%16;
var high:number= (Code-low)/16;
Return "%" +hex (high) +hex (low);
}
Private function Hex (code:number): string{
Switch (code) {
Case 10:
return "A";
Break
Case 11:
return "B";
Break
Case 12:
return "C";
Break
Case 13:
return "D";
Break
Case 14:
return "E";
Break
Case 15:
return "F";
Break
Default
return String (code);
Break
}
}
}

Let's test it again.

Import Lm.utils.LUTF;
var u=new lutf ();
var s:sound=new Sound (this);
S.loadsound ("Dxh.mp3", false);
S.onid3=function () {
var songname:string=_root.u.toutf (this.id3.songname);
Trace (Songname);
}

Output results:
Ding fragrant flowers
To this end we have solved this garbled problem, the use of this technique can also solve other garbled problems!



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.