Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 725
  • Last Modified:

case insensitive strings compare

Hi, experts.
Recently, I've encountered a problem of comparing 2 strings containing German special symbols, like "oäüß"
and "ÖÄÜß". Inside the code i reffer to strings as usual ANSI. I used _stricmp - it doesn't work of course, then I tred to translate strings to Unicode and compare them:

 char* ch1 = "oäüß";
 char* ch2 = "ÖÄÜß";

 int len1 = strlen(ch1);
 int len2 = strlen(ch2);

 WCHAR mbt_out1[256];
 WCHAR mbt_out2[256];

 int nCodePage = 1252;//(I tried also CP_ACP = 0)
 MultiByteToWideChar(nCodePage, 0, ch1, len1, mbt_out1, len1);
 MultiByteToWideChar(nCodePage, 0, ch2, len2, mbt_out2, len2);

 int nCompRes = wcsicmp(mbt_out1, mbt_out2);

Nothing works, i.e. nCompRes != 0. My Windows is not German, but German language is installed.
Please, help who can. Thnx in advance.
0
alex_followap
Asked:
alex_followap
  • 2
  • 2
1 Solution
 
jkrCommented:
Try to set the CRT locale to German, e.g.

char* pszPrevLocale = setlocale( LC_ALL, "German" );

_stricmp ( string1, string2);

// restore locale settings
setlocale( LC_ALL, pszPrevLocale );

0
 
burcarpatCommented:
for an excellent discussion of the topic see matt austern's "how to do case-insensitive string comparison" at,

    http://www.lafstern.org/matt/col2_new.pdf

this article, which was originally published in may 2000 issue of the c++ report, explains locales and facets and their use in c++ in a rather detailed manner and suggests two almost perfect comparators for general and performance use.  furthermore, it also explains exceptions such as german's B and SS conversion, etc.
0
 
alex_followapAuthor Commented:
Thank you, jkr. It works, but why after i translated strings into Unicode using MultiByteToWideChar it doesn't work? As far as i know, Unicode must be independent on locale.
0
 
jkrCommented:
>>but why after i translated strings into Unicode using MultiByteToWideChar it doesn't work?

I assume the codepage being the culprit. using CP_UTF8 *should* solve the problem. But, there is no need to use UNICODE at all.

0
 
alex_followapAuthor Commented:
Thank yu, it worked.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now