Link to home
Start Free TrialLog in
Avatar of cplau
cplau

asked on

String Compare???

Hi All,

I want to ask how to use the following function after
define _MBCS and include <mbstring.h> in my program.
/////////////////////////
int _mbscmp(const unsigned char *string1, const unsigned char *string2 );
////////////////////////

When I have two CString objects, I compare the two string by

_mbscmp(string1,string2);

But the following error occurs:

Cannot conver parameter 1 from CString to const unsigned char*.

Can anyone teach me how to use this function?
Thanks!!!
Avatar of yonat
yonat

Why not use CString::Compare() ?
Avatar of cplau

ASKER

Dear yonat,

I want to make sure that if a CString contain both single and double byte characters....

Will the function CString::Compare() work properly also? I am afraid that it will mix up the single byte character and double byte character.

Thanks!!!


I agree with yonat, do this instead:
"JUST AN EXAMPLE"

int result;
result = string1.Compare(string2);

Compare returns 0 when identical, non-0 other-wise.

The reason why you get the error message is that _mbscmp only accepts type char* not the class CString as input.
As such it would work for the following code:

char* string1, string2;
int result;

strcpy(string1, "abc");
strcpy(string2, "bac");
result = mbscmp(string1,string2);
Convert CString to const unsigned char*.
Say you have CString cstr;
(LPCTSTR)cstr converts it to const unsigned char*.
 
Avatar of cplau

ASKER

Dear yonat and billyh,

I want to make sure that if a CString contain both single and double byte characters....

Will the function CString::Compare() work properly also? I am afraid that it will mix up the single byte character and double byte character.

Thanks!!!
Could you please explain what you mean by a single byte character and a double byte. Do you mean this:

char single_byte;
char double_byte[2];
Avatar of cplau

ASKER

Dear billyh,

The meaing of double bytes character is for the language using large character set.
For English, it only need one byte character.
But in Japan, China, etc. the character set is much larger.

Under MBCS, characters are encoded in either one or two bytes. In two-byte characters, the first, or "lead-byte," signals that both it and the following byte are to be interpreted as one character.

Do you understand what I am asking now?
That the CString may conatin both  ASCII character and Japanese character......
I want to know will the function CString::Compare() will mix up the ASCII character and
Japanese character?

Thanks
I suggest using this.  You can also compare any part of any string.

char String1[x];
char String2[x];
or CString String1; etc

1st. example. Straight compare

strncmp(String1, String2, Number of Letter to compare);

2nd. example.  Different part compare;

1.Hello 2.HiHello

strncmp(String1, String2+2, 6);

this should work.


If you want to use YOUR one, use
unsigned char String1;
unsigned char String2;  that will also solve your problem, but just wastes memory.. :)
any queries, questions, just E-Mail G_duToit@hotmail.com
I have been looking at the online help about MBCS. CString supports both Unicode and MBCS. As such I think that the Compare member function is able to identify both codes during Comparison using Polymorphism. The only way you can know if it does work is to try and find out.
I think I do not make much sense in my last sentence. What I mean is that code using CString.Compare() and then debug your application to see if it gives you the desired results. This is how you learn sometimes.
CString uses TCHAR. TCHAR is #defined is a single byte character for non-unicode programs, and as a double byte character for unicode programs.

Let me see if I understand you correctly: Your program will have both non-unicode strings and unicode strings _at_the_same_time_ and you may need to compare a unicode string with a non-unicode string? Did I get it right?
Avatar of cplau

ASKER

Dear billyh,

The meaing of double bytes character is for the language using large character set.
For English, it only need one byte character.
But in Japan, China, etc. the character set is much larger.

Under MBCS, characters are encoded in either one or two bytes. In two-byte characters, the first, or "lead-byte," signals that both it and the following byte are to be interpreted as one character.

Do you understand what I am asking now?
That the CString may conatin both  ASCII character and Japanese character......
I want to know will the function CString::Compare() will mix up the ASCII character and
Japanese character?

Thanks
Avatar of cplau

ASKER

Dear yonat,

My program will not use UNICODE....it just use the Double byte character set.....

You are right that my program will sometimes use one byte character set and also
use two bytes character set....

I am not sure whether CString::Compare() can identitfy which character is one byte and two bytes during the comparsion. Or it will just treat the string as a normal string which contain only ASCII code only......it will treate one two byte character as two characters...

Can you give me advice?


I have found another function that might be helpful to you. Has something about Hiragana and Katakana characters.

The function is called CompareString. Read about it, it is on your Microsoft Developers Studio's online-help. Check it out and see if that's what you want.
Why don't you use something like:

int nRes;
CString cs1, cs2;
..
#ifdef _MBCS
       nRes = _mbscmp( (const unsigned char*)(LPCTSTR)cs1,
                                   (const unsigned char*)(LPCTSTR)cs2);
#else
       nRes = cs1.Compare(cs2);
#endif

perhaps another #if _UNICODE  .....

dib


to avoid the compile error , just do this.
CString string1,string2;

_mbscmp((const unsigned char*)((LPCSTR)string1) ,(const unsigned char*)((LPCSTR)string2));


Avatar of cplau

ASKER

Dear wyy_cq,

After using the function _mbscmp, do I need to concern that the comparsion will mix up the single byte character and double byte character?

Thanks!!!
ASKER CERTIFIED SOLUTION
Avatar of billyh
billyh

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I see now. CString::Compare will not do in this case. I never had to do this myself, but I think CString::Collate will do what you want.
Avatar of cplau

ASKER

Thanks for all the advice given!!!
Wow that's a nica surprise thanx.