?
Solved

String Compare???

Posted on 1998-12-07
21
Medium Priority
?
400 Views
Last Modified: 2013-11-20
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!!!
0
Comment
Question by:cplau
  • 7
  • 7
  • 3
  • +4
21 Comments
 
LVL 5

Expert Comment

by:yonat
ID: 1325867
Why not use CString::Compare() ?
0
 

Author Comment

by:cplau
ID: 1325868
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
 
LVL 1

Expert Comment

by:billyh
ID: 1325869
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Expert Comment

by:stkul
ID: 1325870
Convert CString to const unsigned char*.
Say you have CString cstr;
(LPCTSTR)cstr converts it to const unsigned char*.
 
0
 

Author Comment

by:cplau
ID: 1325871
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
 
LVL 1

Expert Comment

by:billyh
ID: 1325872
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
 

Author Comment

by:cplau
ID: 1325873
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
 
LVL 1

Expert Comment

by:The_Brain
ID: 1325874
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
 
LVL 1

Expert Comment

by:billyh
ID: 1325875
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
 
LVL 1

Expert Comment

by:billyh
ID: 1325876
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
 
LVL 5

Expert Comment

by:yonat
ID: 1325877
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
 

Author Comment

by:cplau
ID: 1325878
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
 

Author Comment

by:cplau
ID: 1325879
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
 
LVL 1

Expert Comment

by:billyh
ID: 1325880
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
 
LVL 4

Expert Comment

by:nil_dib
ID: 1325881
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
 
LVL 2

Expert Comment

by:wyy_cq
ID: 1325882
to avoid the compile error , just do this.
CString string1,string2;

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


0
 

Author Comment

by:cplau
ID: 1325883
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
 
LVL 1

Accepted Solution

by:
billyh earned 200 total points
ID: 1325884
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
 
LVL 5

Expert Comment

by:yonat
ID: 1325885
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
 

Author Comment

by:cplau
ID: 1325886
Thanks for all the advice given!!!
0
 
LVL 1

Expert Comment

by:billyh
ID: 1325887
Wow that's a nica surprise thanx.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses
Course of the Month14 days, 21 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question