How to check if a string consists only of chars, which can be successfully encoded in ISO 8859-1? Or in other words - how to find "illegal"/"not ISO 8859-1 compatible" chars in a string?
Asked
Active
Viewed 9,870 times
4
-
Would [this post](http://stackoverflow.com/questions/1025332/determine-a-strings-encoding-in-c-sharp) help? – Mechanical Object Jul 26 '13 at 09:43
-
@MechanicalObject: Probably not, if it is already a C# String (and not raw bytes). – Thilo Jul 26 '13 at 09:45
3 Answers
15
Try this:
private static bool IsValidISO(string input)
{
byte[] bytes = Encoding.GetEncoding("ISO-8859-1").GetBytes(input);
String result = Encoding.GetEncoding("ISO-8859-1").GetString(bytes);
return String.Equals(input, result);
}
This answer is based on an answer of this Java question (my code is the C# equivalent): http://www.velocityreviews.com/forums/t137810-checking-whether-a-string-contains-only-iso-8859-1-chars.html

ProgramFOX
- 6,131
- 11
- 45
- 51
-
1
-
2@netblognet You're welcome! I also looked at your code, but it looks "dangerous" because you can't be 100% sure that non-ISO chars will give a question mark. My code is also faster. – ProgramFOX Jul 26 '13 at 10:18
-
1Great answer. Small suggestion even if it is a really old post. Instead of Encoding.GetEncoding("ISO-8859-1") use Encoding.Latin1 – Oliver Voutat Jul 14 '22 at 08:45
0
You could setup an array or list of valid characters and then iterate through your string to check if each of them exists in your list of valid characters. The list can be created by adding all valid latin-1 characters to it.

Ron Deijkers
- 2,791
- 2
- 22
- 28
0
I came up with this idea. Might this be possible?
private static bool IsValidISO(string input)
{
foreach (char c in input)
{
Encoding iso = Encoding.GetEncoding("ISO-8859-1");
Encoding utf8 = Encoding.UTF8;
byte[] isoBytes = iso.GetBytes(c.ToString());
byte[] utfBytes = Encoding.Convert(iso, utf8, isoBytes);
string convertedC = utf8.GetString(utfBytes);
if (c != '?' && convertedC == "?")
return false;
}
return true;
}

netblognet
- 1,951
- 2
- 20
- 46