Well, you are dipping your toes there into a very hard problem. For
the various byte encodings, there really is little beyond wizzadry
heuristics to use to deduce which encoding is being used.
At least for UTF-8 one can say that the stream of bytes either is valid
as a UTF-8 encoding or is invalid as a UTF-8 encoding.
The invalid is simple, whatever the byte stream is, it is not UTF-8.
The 'valid' one is tricker. It is possible, although highly unlikely,
that some random stream of bytes just happens to be structured such
that it is also a valid UTF-8 encoding. So even 'validity' for UTF-8
isn't an absolute. Although I'll admit that for this one, the chance
of this happening is remote enough you can simply ignore it in the
general sense.
But with the multitude of byte encodings, if you get a byte stream
containing a character 0xBF, how can you deduce that it was meant to be
"INVERTED QUESTION MARK" from ISO-8859-1 vs "LATIN SMALL LETTER Z WITH
DOT ABOVE" from ISO-8859-3 vs. a top right corner line draw character
from the old IBM code page 437 from the DOS days? That's where it gets
really really tricky....