Ejemplo n.º 1
0
        /// <summary>
        /// Analyse File Encoding
        /// </summary>
        /// <param name="path">File Path</param>
        /// <returns>File Encoding</returns>
        public static Encoding AnalyseEncoding(string path)
        {
            if (File.Exists(path))
            {
                var bytes = File.ReadAllBytes(path);

                var enc = EncodingAnalyser.Analyse(bytes);

                return(enc);
            }
            else
            {
                throw new ArgumentException($"File Not Found : {path}");
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Convert To Managed String
        /// </summary>
        /// <param name="char_ptr">Native Char Pointer</param>
        /// <param name="encoding">Encoding</param>
        /// <returns>Managed String</returns>
        public static string ToManagedString(this IntPtr char_ptr, string encoding = "utf-8")
        {
            if (char_ptr == IntPtr.Zero)
            {
                return(null);
            }
            else
            {
                var x = 0;

                var bytes = new List <byte>();

                unsafe
                {
                    var byte_ptr = (byte *)char_ptr;

                    while (byte_ptr[x] != 0)
                    {
                        x++;
                        bytes.Add(byte_ptr[x]);
                    }

                    var byte_array = bytes.ToArray();

                    var enc = EncodingAnalyser.Analyse(byte_array) ?? Encoding.GetEncoding(encoding);

                    var len = enc.GetCharCount(byte_ptr, x);

                    if (len == 0)
                    {
                        return(string.Empty);
                    }
                    else
                    {
                        var str_buf = stackalloc char[len];

                        var str_len = enc.GetChars(byte_ptr, x, str_buf, len);

                        var str = new string(str_buf, 0, str_len);

                        return(str);
                    }
                }
            }
        }