Solved

String Compare???

Posted on 1998-12-07
21
360 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
Comment Utility
Why not use CString::Compare() ?
0
 

Author Comment

by:cplau
Comment Utility
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
Comment Utility
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
 

Expert Comment

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

Author Comment

by:cplau
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 5

Expert Comment

by:yonat
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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 50 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks for all the advice given!!!
0
 
LVL 1

Expert Comment

by:billyh
Comment Utility
Wow that's a nica surprise thanx.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
repeatSeparator  java  challenge 13 56
Named range not carried over 10 56
sum28 challenge 31 95
Problem to open Excel file 15 35
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

762 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now