|
All knows how to scold the person who suggested. Stupids is there any one who scold the autor suggested the other solution. Put your solution for the above scenario if you know. otherwise shup up all your stupid mouth.
|
|
|
|
|
Okay, we all agree that you can't use CString to hold binary data. That's a given. Let's see if we can help him anyway. I'm convinced he's not a troll. (He's stubborn, arrogant, and inexperienced, but not a troll.)
If I understand him, he's trying to use an already existing object, which he did not write himself, and which only accepts CStrings, to store encrypted data. I think this is why he's been saying he's "forced" to use CString.
If that's the case, he's got a little problem, because, as we all know, you can't store binary data in a CString. But, what he CAN do is convert that binary data into characters, and then pass that to his object.
An example: let's say we have an array of five BYTEs that represent an encrypted password:
ar[0] = 32
ar[1] = 0
ar[2] = 17
ar[3] = 254
ar[4] = 133
Note that this is not a string, so it's not NULL terminated; but, it just by chance happens to have a value of zero as the second member (which would hose a CString, we all agree). Now, what we have to do is translate this array into something that can be stored in a CString.
Since I'm lazy, I choose to translate each BYTE into a three-character value; I'm sure more clever programmers could come up with more efficient methods. Anyway, using the example above, we convert this into one big CString:
CString sWhole;
for (int i = 0; i < 5; ++i) {
CString sPiece;
sPiece.Format("%0d", ar[i]);
sWhole += sPiece;
}
(Forgive my bad style.) At this point we have a CString, 'sWhole', that we can send to his third-party encryption object. It looks like:
"032000017254133"
(I know, I know, it's terribly wasteful, but on a machine with an 80 Gig hard drive, I have better things to worry about. Unless you're talking about more than a few hundred KB of data.)
It is left as an exercise for the OP to figure out how to reverse this process; that is, to translate 'sWhole' back into an array of BYTEs.
|
|
|
|
|
Hi..
I had the same idea.
But use base64 encoding for it !
it convert 8 bit datas to 6 bits (2 bit wastet for one byte) ;(
But it will be better
Cya
|
|
|
|
|
If CString holds binary (see the BOOK "Professional MFC with Visual C++ 5" from "Mike Blaszczak" ISBN 1-861000-14-6 pages 445 and 455) (and it works very well because I have tested and it is working for me with no problems), why put all of this overhead ?
If you can use array, great choice.
|
|
|
|
|
Of course it holds binary. There is only binary information. BUT, CString class is written to handle ASCII and Unicode strings, not random binary data. Strings end to null-terminator, which you should have learned when learning C++. So CString works as it is intended to.
Do you really think that a base class which has been used widely for a decade has this bug this obvious?
|
|
|
|
|
Oh man, you said very well, There is only binary information .
I just think that a class that writes 100 bytes to a file, must read the same 100 bytes from the file not 10 or 20. Why this is so difficult to accept ?
I'm showing only this point, not the if CString was designed to be an wrapper to the 'C' style strings or not.
Why people insists in offend me ? Were is the human being intelligence ?
I don't think that only Saddam Russein and your crew are intolerant, the intolerance today, is being praticed even in a message board ... I think that what is being showed here is not a defense of a point of view but a intolerance about an issue.
|
|
|
|
|
You do not have to bring political figures off the walls to this debate. Please, stay on topic.
To recap things: CString is intended for handling and manipulating textual data, not _random_ binary data. Use CString for handling and manipulating textual data, not _random_ binary data.
As you might have already guessed, there are rules what kind of stuff CString holds, one rule is that its content ends to a null-terminator. So if the class that is meant to handle this textual data which ends to a \0, considers mark \0 end of content it works correctly.
There are methods that allows you to copy a range of characters to a CString, but it is intended to be used in places where you want less characters than the original string holds, not more!
|
|
|
|
|
I give up. You're an idiot.
|
|
|
|
|
Give the guy a break. Obviously he feels that there is some reason that he must use CString in this way. I think it is clear that he understands how CString is "supposed" to be used, but, maybe because of system design, he is forced to represent some bit of data as a CString and cannot change it to a more appropriate type. We've all encountered this before. It is clear that this is not a bug in MFC, but a representational mismatch. What you should probably do is define a derived class of CString (CBinaryString maybe) and define << and >> operators on CArchive for that type using your proposed serialization methods (though this may only work in the larger context of a system redesign making this a moot point). You guys should appreciate that he is trying to contribute something that he deems to be of value and help him out instead of flaming him. Go to slashdot if you're going to be juvenile.
|
|
|
|
|
Did you even *read* my lengthy post? I *know* what his problem is: he's having to use a class that only accepts CStrings, and he's trying to put encrypted data into it. If you had read all his posts, as I did, you would have figured that out.
I, and many other people, have attempted to explain to him many, many, many, many times what's going on, but he refuses to listen. Again, if you will read all the posts, I think you'll be amazed at how aggressively stupid this person is. For example, my post provided a solution to his problem, and he's completely ignored it.
So, yes, I flamed him, after trying (harder than anyone else, BTW) to understand his problem and provide a workaround. His sin is not that he's an idiot - we're all stupid about somthing. His sin is that he refuses to listen, think, and learn. The guy doesn't deserve a break.
|
|
|
|
|
I actually did read every single post, but I find it much less productive to waste time and bandwidth calling people names. Clearly he is using CString in a traditionally wrong way, but so what? Why am I even posting this? Clearly what I am saying is falling on deaf ears. My intuition is that you can stick anything into a CString that you want as long as you understand what you are doing. leandrobecker clearly knows that what he is doing is not strictly guaranteed to work but with an understanding of the workings of CString he can do whatever he wants. This guys dererves more credit than you are giving him. I agree that to call it a bug is incorrect, but we stopped arguing semantic points 10 flames ago. Let it go guys.
|
|
|
|
|
Your intuition is incorrect. You cannot stick anything into a CString, even if you understnd what you are doing.
Also, leandrobecker clearly has no idea what he/she is doing, and annot do whatever he/she wants with a CString.
Finally, if you think the difference between a "bug" and "misusing a class" is a semantic point, there's not much more I can say.
But, I agree we should let it go...
|
|
|
|
|
Exactly.
Although CString is designed for use with null terminated strings, it can be used for binary data if appropriate and if you're aware of the limitations.
I've only twice had to do so (when working on Virtual Instrument drivers for a VXIbus Arbitary Waveform Generator and a Waveform Digitiser) but on that occasion it was a lifesaver as the interface we had to talk to supported only simple types and CString. It worked like a charm.
Anna
Homepage | My life in tears
"Be yourself - not what others think you should be"
- Marcia Graesch
"Anna's just a sexy-looking lesbian tart"
- A friend, trying to wind me up. It didn't work.
Trouble with resource IDs? Try the Resource ID Organiser Visual C++ Add-In
|
|
|
|
|
You're not helping Larry.
Give it up or vent in the Soapbox please.
Anna
Homepage | My life in tears
"Be yourself - not what others think you should be"
- Marcia Graesch
"Anna's just a sexy-looking lesbian tart"
- A friend, trying to wind me up. It didn't work.
Trouble with resource IDs? Try the Resource ID Organiser Visual C++ Add-In
|
|
|
|
|
Funny, mother never mentioned you...
I did give it. I have given up. I give up.
|
|
|
|
|
|
Relax everyone. It is perfectly ok to use CString to hold data that contains multiple null characters. But you have to be careful when using some methods (conversions/casts between pointers). I think it is correct to call the problem described in this article a bug or a missing feature, depending on how you look at it.
Ok, you don't believe me, but I heard it a few years ago from the master of VC++/MFC, Mike Blaszak (spelling?) himself. He said there is even a CString constructor just for this situation:
<br />
CString( LPCTSTR lpch, int nLength );<br />
throw( CMemoryException );<br />
|
|
|
|
|
Mike doesn't mean that! You idiot!
|
|
|
|
|
see the BOOK "Professional MFC with Visual C++ 5" from "Mike Blaszczak" ISBN 1-861000-14-6 pages 445 and 455 to see who is wrong ...
|
|
|
|
|
I shouldn't response to you anymore but I was worry that your misunderstanding of string type in C would affect others, so here I am again.
I hope this would be my last message on...whatever you want to say again on "string". At the end of the days, it is YOU who gonna suffer. You should be glad that now there is WWW to guide you around on C/C++. 10 years ago, I would have to bang my head on the wall and hope for god to help.
First of all, I didn't have the book, so I am gonna trust 100% on you regarding the book. But I don't need the book to understand what Mike has to mean. Now, read my comment carefully, I said: "Mike does not mean that". This mean you can break the zero-terminated rule of string type, but that DOESN'T mean C/C++ language encourage you to do so! Neither this is a BUG in any other supporting class which obey zero-terminated string rule. If a stream of characters no longer have '\0' to indicate it is the end, IT DOES NOT QUALIFY AS A STRING TYPE ANYMORE, IT IS NOW A BINARY STREAM. That particular constructor can be used as:
CString sz( str_source, str_source.GetLength() );
Or:
BYTE binary_object[256] = {0}
CString sz( (LPCTSTR)binary_object, sizeof(binary_object) );
Mike present the text in a sense that it illustrate the flexibility of CString, but he is DEFINITELY not saying CArchive should now consider a C string to have multiple zero-terminated marker!! Neither he is suggesting CString should now consider equivalent to CComBSTR.
Think again, is CHAR and BYTE the same? Suit yourself and leave us alone!
|
|
|
|
|
If you want to throw insults I suggest you do so in the Soapbox so we can all tell you what we think of your attitude in an appropriate manner.
Lighten up.
Anna
Homepage | My life in tears
"Be yourself - not what others think you should be"
- Marcia Graesch
"Anna's just a sexy-looking lesbian tart"
- A friend, trying to wind me up. It didn't work.
Trouble with resource IDs? Try the Resource ID Organiser Visual C++ Add-In
|
|
|
|
|
"Be yourself - not what others think you should be"
- Marcia Graesch
If you are looking for attention, I suggest go somewhere else.
|
|
|
|
|
Excuse me?
I merely suggested you move to the Soapbox if you want to throw insults around.
What makes you think I'd even want your attention anyway?
Anna
Homepage | My life in tears
"Be yourself - not what others think you should be"
- Marcia Graesch
"Anna's just a sexy-looking lesbian tart"
- A friend, trying to wind me up. It didn't work.
Trouble with resource IDs? Try the Resource ID Organiser Visual C++ Add-In
|
|
|
|
|
|
I think you're right, and so is the author of this article.
He changed the title and that should make everyone happy!
CString is storing the string length inside, and it always uses that size except when copying the string from somewhere else, be it a C string or a CArchive.
It may or may not be considered a bug, but probably, as MS would say, this is a "feature".
Paolo
------
Why spend 2 minutes doing it by hand when you can spend all night plus most of the following day writing a system to do it for you? - (Chris Maunder)
|
|
|
|
|