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!!!
cplauAsked:
Who is Participating?
 
billyhConnect With a Mentor Commented:
I know you are going to reject this, but cplau unless you try-out some of the advices given to you and actually see what happens when you debug your application then your question might go unanswered.

The thing is it is a simple question, I have never coded using MBCS, but with all the advice given you I think by now I would have figured out a solution myself if I were you. So just try anyone of the suggestions and see what happens.
0
 
yonatCommented:
Why not use CString::Compare() ?
0
 
cplauAuthor Commented:
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!!!


0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
billyhCommented:
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);
0
 
stkulCommented:
Convert CString to const unsigned char*.
Say you have CString cstr;
(LPCTSTR)cstr converts it to const unsigned char*.
 
0
 
cplauAuthor Commented:
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!!!
0
 
billyhCommented:
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];
0
 
cplauAuthor Commented:
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
0
 
The_BrainCommented:
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
0
 
billyhCommented:
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.
0
 
billyhCommented:
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.
0
 
yonatCommented:
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?
0
 
cplauAuthor Commented:
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
0
 
cplauAuthor Commented:
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?


0
 
billyhCommented:
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.
0
 
nil_dibCommented:
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


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

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


0
 
cplauAuthor Commented:
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!!!
0
 
yonatCommented:
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.
0
 
cplauAuthor Commented:
Thanks for all the advice given!!!
0
 
billyhCommented:
Wow that's a nica surprise thanx.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.