AcTools.Utils.Helpers.UTF8Checker.IsUtf8 C# (CSharp) Method

IsUtf8() public static method

public static IsUtf8 ( byte buffer, int limit = int.MaxValue ) : bool
buffer byte
limit int
return bool
        public static bool IsUtf8(byte[] buffer, int limit = int.MaxValue) {
            var position = 0;
            var bytes = 0;
            var length = buffer.Length;
            while (position < length && position < limit) {
                if (buffer[position] > 0x7F) {
                    if (!IsValid(buffer, position, length, ref bytes)) {
                        return false;
                    }
                    position += bytes;
                } else {
                    position++;
                }
            }
            return true;
        }

Usage Example

Ejemplo n.º 1
0
 public static string ToUtf8String([NotNull] this byte[] bytes)
 {
     return((UTF8Checker.IsUtf8(bytes, 200) ? Encoding.UTF8 : Encoding.Default).GetString(bytes));
 }