Click here to Skip to main content
13,046,464 members (72,231 online)
Rate this:
Please Sign up or sign in to vote.
See more:
CString is not accepting the NULL values in between? I am reading the image file, while reading am storing it in char and while copying the value to CString, It accepts the characters till the first NULL characters, rest values are ignored. Help me........
Posted 15-May-11 23:54pm
CPallini 19-May-11 7:54am
Why do you need that (i.e. there might be better ways to achieve your final target)?
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

In a C-style string, the NULL character means "end of string". The CString object will display all characters untill the NULL character is found. But it doesn't mean the next characters are not stored or lost.

For example:
CString foo;
foo += 'a';
foo += 'b';
foo += '\0';
foo += 'c';
foo += 'd';
//will contain 'c'
char c = foo[3];
//will contain 'd'
char d = foo[4];
//l equals 5.
int l = foo.GetLength();

In the above code, the debugger will display "ab" for the foo variable, however, c will contain 'c' and d will contain 'd' as expected.

But if you want to store a block of memory somewhere, I suggest not to use CString class. Use a byte or char array instead (CByteArray for example, or regular arrays).
Nithin Sundar 16-May-11 6:35am
This certainly doesn't deserve a downvote! Not when the solution poster has taken valuable time to explain a concept which is well within the scope of this question.
Olivier Levrey 16-May-11 6:39am
The down-voter probably voted on the first revision of my answer while I was changing it:
my first answer was not so detailed and could be misunderstood.
Thank you Nithin.
Stefan63 16-May-11 10:14am
Upvoted for balance ;) This is definitely a good answer.

My advice would have been to use a different type altogether, such as CByteArray - but you already suggested that as well. Good work!
Olivier Levrey 16-May-11 10:26am
Thank you very much Stefan.
Albert Holguin 16-May-11 10:36am
right +5
SAKryukov 20-May-11 15:20pm
Absolutely correct, my 5.
Pay attention, I also answered null character is allowed in CString but face lack of understanding.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

I don't think this is true. All non-idiotic string type perfectly accept null characters.

(A null-terminated string is arguably idiotic; this is one of the most idiotic inventions of human kind of all times and peoples. At the same time, this type is one of the most used. Not good for us. All non-idiotic string types are length-prefixed in persistent presentation or just contain a length descriptor.)

Now, the problem is not the type but some function you call which may rely on null character. There are millions of such functions, I don't know which one caused the problem. Read the help pages on each one you use or see it under the debugger.

CPallini 19-May-11 7:49am
Well, that is your opinion. The terminating NULL char has its drawbacks, I agree. But the lenght-prefix (aka 'Pascal' style) has (and above all had) its drawbacks too. For instance, what should be the size of the prefix? Would such string be portable to microcontrollers?
SAKryukov 20-May-11 15:17pm
Is this is a problem, but not such idiotic as null terminated.
This is exactly the same kind of problem as the size of integer type on different systems. Could be separate types on different systems, big deal. Null character is
Your question about micro-controllers is irrelevant. Is the null-terminated idea was never used, it was not used anywhere, not in micro-controllers, nowhere.

No, this is not a matter of my taste or something like that. I'm talking objective criteria. If you think that real-life technological culture is rational, think again. It's full of misconceptions and even idiotic decisions as this one (this is not the only one).

Now, if we discussed just my answer about CString, it is also irrelevant. This type accept null string in the middle. Want to test it? My answer is correct anyway.

Thank you.
CPallini 20-May-11 16:20pm
Nope. Microcontroller question is relevant. There isn't a portable (meaningful) way to define length prefixed strings for both a PC and, say, a 8 bit MCU. That said, what are the HUGE advantages of lenght-prefixed strings to mark the other choice 'idiotic'? I see only some conveniences, nothing more (please remember OOP isn't the way, is just a way of programming).
CPallini 20-May-11 16:21pm
Nope. NULL-terminating is a portable way to handles strings. And microcontroller question is relevant. How could you port the PC length-prefixed string to an 8 bit MCU, in a meaningful way?
And what are the HUGE advantages of length-prefixed strings? I see some conveniences, nothing more.
SAKryukov 21-May-11 0:03am
In brief, you're too preoccupied with null-termination. It's only portable in the framework of currently established practice, not more. If you try to explain this is portable you will not give an argument. The benefit is not convenience. It's as fundamental as direction of time if you will. Direction of reading data. Null-termination is huge performance hit and also a risk. Further discussion will need considering algorithms. You can consider them yourself.
CPallini 21-May-11 5:06am
I'm not worried about. However it is portable, period (like C). "It is fundamental" without proof is an opinion, not a fact. However, like with 'the registry' I don't want to fight a religious war here.
SAKryukov 21-May-11 0:04am
Wait a minite... We already discussed integer type for money, did we. Oh, not anymore... :-)
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

Check this link and convert accordingly.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.170713.1 | Last Updated 16 May 2011
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100