using System;
using System.Text;
public class Program
{
static bool IsValidUTF8(byte[] s) {
var encoding = new UTF8Encoding(false, true);
try {
encoding.GetCharCount(s);
}
catch (DecoderFallbackException) {
return false;
}
return true;
}
public static void Main(string[] args)
{
byte[] arr1 = Encoding.UTF8.GetBytes("Hello, 世界");
byte[] arr2 = { 0xa3, 0xed, 0xfd };
Console.WriteLine(IsValidUTF8(arr1));
Console.WriteLine(IsValidUTF8(arr2));
}
}
/*
run:
True
False
*/