What is Nchardet
NchardetYesMozillaAutomatic character encodingProgramLibraryChardetOf. NetIt is transplanted fromJchardet,ChardetOfJavaTo encode a given streaming.Probe.
NchardetHow it works
NchardetCompare the input characters one by one to guess the encoding. Because it is a guess, it may not be fully recognized. If the input character cannot be determined by the correct encodingNchardetA set of possible encoding values are provided.
For exampleHow to UseNchardet
To useNchardetTo detect the encoding, follow these steps.
1And constructed using the language clues.DetectorClass instance object.
2.IcharsetdetectionobserverThe interface object is called as a parameter.DetectorClassInitMethod.
3And pass in the pipeline stream to be tested for encoding detection.
4, CallDetectorClassDataendMethod.
5To obtain results or possible result sets.
The language clue is an integer. The available language clues are as follows:
1.Japan
2.Chinese
3.Simplified Chinese
4.Traditional Chinese
5.Korean
6.Dont know (Default)
IcharsetdetectionobserverThere is only one interfaceNotifyMethod, whenNchardetWhen the engine determines that it has detected the correct encoding, it will call thisNotifyMethod, the user program canNodifyGet notification in method (rewriteIcharsetdetectionobserverInterfaceNotify).
CodeInstance:
// Implement icharsetdetectionobserver Interface
Public Class Mycharsetdetectionobserver:
Nchardet. icharsetdetectionobserver
{
Public String Charset = Null ;
Public Void Notify ( String Charset)
{
Charset = Charset;
}
}
Int Lang = 2 ; //
// Instantiate the detector with the specified language Parameter
Detector det = New Detector (Lang );
// Initialization
Mycharsetdetectionobserver CDO = New Mycharsetdetectionobserver ();
Det. INIT (CDO );
// Input stream
Uri URL = New Uri ("http: // Cn.yahoo.com ");
Httpwebrequest request =
Httpwebrequest) webrequest. Create (URL );
Httpwebresponse response =
(Httpwebresponse) request. getresponse ();
Stream stream = Response. getresponsestream ();
Byte [] Buf = New Byte [ 1024 ];
Int Len;
Bool Done = False ;
Bool Isascii = True ;
While (Len = Stream. Read (BUF, 0 , Buf. Length )) ! = 0 ){
// Whether it is ASCII Encoding
If (Isascii)
Isascii = Det. isascii (BUF, Len );
// if it is not ASCII, if the encoding is not determined, continue to test
If ( ! isascii & ! done)
done = Det. doit (BUF, Len, false );
}
Stream. Close ();
Stream. Dispose ();
//Call the datend method,
//If the engine considers that the correct encoding has been detected,
//The notify method of icharsetdetectionobserver will be called at this time.
Det. dataend ();
If (Isascii ){
Console. writeline ( " Charset = ASCII " );
Found = True ;
}
Else If (CDO. charset ! = Null )
{
Console. writeline ( " Charset = {0} " , CDO. charset );
Found = True ;
}
If ( ! Found ){
String [] Prob = Det. getprobablecharsets ();
For ( Int I = 0 ; I < Prob. length; I ++ ){
Console. writeline ( " Probable charset = " + Prob [I]);
}
}
Console. Readline ();
ObtainNchardet
NchardetLibrary (. NET 1.1 and. NET 2.0, DLL)
Nchardet source code (The development environment isSharpdevelop 2.0)
Copyright description
This program is transplanted fromJchardetFor copyright description, seeJchardet,C #Code usageBSDAuthorization.
Related Links
Mozilla chardet http://www.mozilla.org/projects/intl/chardet.html
Mozilla chardet download http://lxr.mozilla.org/mozilla/source/intl/chardet/ BR> jchardet http://jchardet.sourceforge.net/
sharpdevelop http://www.icsharpcode.net/OpenSource/SD/
automatic identification of character set encoding jchardet http://hedong.3322.org/archives/000361.html