public static Encoding[] DetectInputCodepages(byte[] input, int maxEncodings) { if (maxEncodings < 1) { throw new ArgumentOutOfRangeException("at least one encoding must be returend", "maxEncodings"); } if (input == null) { throw new ArgumentNullException("input"); } if (input.Length == 0) { return(new Encoding[] { Encoding.ASCII }); } if (input.Length < 256) { byte[] newInput = new byte[256]; int steps = 256 / input.Length; for (int i = 0; i < steps; i++) { Array.Copy(input, 0, newInput, input.Length * i, input.Length); } int rest = 256 % input.Length; if (rest > 0) { Array.Copy(input, 0, newInput, steps * input.Length, rest); } input = newInput; } List <Encoding> result = new List <Encoding>(); IMultiLanguage2 multilang2 = new FMultiLanguageClass(); if (multilang2 == null) { throw new System.Runtime.InteropServices.COMException("Failed to get IMultilang2"); } try { SDetectEncodingInfo[] detectedEncdings = new SDetectEncodingInfo[maxEncodings]; int scores = detectedEncdings.Length; int srcLen = input.Length; SMlDetectCp options = SMlDetectCp.MLDETECTCP_NONE; multilang2.DetectInputCodepage(options, 0, ref input[0], ref srcLen, ref detectedEncdings[0], ref scores); if (scores > 0) { for (int i = 0; i < scores; i++) { result.Add(Encoding.GetEncoding((int)detectedEncdings[i].nCodePage)); } } } finally { Marshal.FinalReleaseComObject(multilang2); } return(result.ToArray()); }
public virtual extern void DetectInputCodepage([In] SMlDetectCp flags, [In] uint dwPrefWinCodePage, [In] ref byte pSrcStr, [In, Out] ref int pcSrcSize, [In, Out] ref SDetectEncodingInfo lpEncoding, [In, Out] ref int pnScores);
public virtual extern void DetectCodepageInIStream([In] SMlDetectCp flags, [In] uint dwPrefWinCodePage, [In, MarshalAs(UnmanagedType.Interface)] IStream pstmIn, [In, Out] ref SDetectEncodingInfo lpEncoding, [In, Out] ref int pnScores);