3

we need to write a function which can check whether a string is valid UTF8 or not. A client sends data (which is supposed to be UTF8) and on the server side, we want to confirm that sent data is really UTF8.

The client is BREW (mobile platform) application and server is written on windows desktop (windows's API).

Is there any API in windows that can help to determine the validity of a UTF8 string?

I have seen something like MultiByteToWideChar() which convert UTF8 to wide character (2 bytes), and if we use this function and it fails, it means that passed string doesn't have UTF8 character/characters.

Deanna
  • 23,876
  • 7
  • 71
  • 156
  • Possible duplicate of [check for invalid UTF8](http://stackoverflow.com/q/6555015/588306) but the answers check manually. – Deanna May 15 '12 at 13:48

1 Answers1

2

You can use the MultiByteToWideChar() function with the MB_ERR_INVALID_CHARS flag and a 0 passed to cchWidechar. If it returns a size then it's valid. If it returns 0, you can then check GetLastError and see if it returns ERROR_NO_UNICODE_TRANSLATION.

Deanna
  • 23,876
  • 7
  • 71
  • 156
  • Good. And checking `GetLastError` is thread-safe. See [here](https://stackoverflow.com/a/3426055/11279879). – Dr. Gut May 29 '21 at 21:12