|
-
May 6th, 2004, 05:27 AM
#1
String Copy
Am trying to copy array "a" into CString, but only value 'a' is copied. I want to put all {'a',0,'b'} into str. How to
please see the code below. This is what I have done
char a[3] ={'a',0,'b'};
CString str(a);
AfxMessageBox(str);
AfxMessageBox displays only 'a'
Thanks in Adv
-
May 6th, 2004, 05:30 AM
#2
I presume you're not compiling in Unicode? The CString constructor has copied the string, which is interpreted as char*'s up to the first null (at a[1]). Basically, you're using CString in a way it wasn't designed. What are you trying to accomplish?
Some cause happiness wherever they go; others, whenever they go.
-
May 6th, 2004, 06:00 AM
#3
Hello
THanks for the reply. I want "a0b" in that CString variable. Thats what the requirement is . THis is not a unicde build
Thanks
-
May 6th, 2004, 06:23 AM
#4
Re: String Copy
Originally posted by spicy_kid2000
Code:
char a[3] ={'a',0,'b'};
Do you mean that or
Code:
char a[3] = {'a', '0', 'b'};
Or are you saying that every other character is a number that you want to insert into your output string? Can we expand it to
Code:
char a[] = {'a', 0, 'b', 1, 'c', 2, 'd', 3};
T
Some cause happiness wherever they go; others, whenever they go.
-
May 6th, 2004, 07:10 AM
#5
No ,
just take one example. a[3] = {'a',0,'b'}
This I want to copy to a string
and string shud give me "a0b"
-
May 6th, 2004, 07:24 AM
#6
Dear, spicy_kid2000!
What you mean:
just take one example. a[3] = {'a',0,'b'}
This I want to copy to a string
and string shud give me "a0b"
is impossible!
Pleas read carefully what Toot replied you:
The CString constructor has copied the string, which is interpreted as char*'s up to the first null (at a[1]).
If you want to get "a0b" (to get character '0' in the string) you should set:
Code:
char a[3] = {'a', '0', 'b'};
or
-
May 6th, 2004, 07:38 AM
#7
ok Sir
I got it
Thanks everybody
it was a mistake from my side
chao
-
May 6th, 2004, 08:34 AM
#8
Originally posted by Toot
The CString constructor has copied the string, which is interpreted as char*'s up to the first null (at a[1]).
I know the OP's original code didn't work because he didn't specify a character of zero, but rather a NULL.
I just thought I would point out that there is a form of the constructor that will work with the original code:
Code:
char a[3] ={'a',0,'b'};
CString str(a, 3);
When copying CStrings around, it uses the fact that you've told it how long the string is to copy itself, not just up to the first NULL in the string.
This means you can use CString to hold and manipulate binary data, which at times can be very convenient and useful.
-
May 6th, 2004, 08:48 AM
#9
I was aware of that constructor but MSDN doesn't state what will happen if there is a null character in the string, and I have to confess, I didn't try it yet . However, as the very next thing spicy_kid2000 does is AfxMessageBox, I had assumed that it was going to be used for string manipulation. Hence the quest for further classification .
T
Some cause happiness wherever they go; others, whenever they go.
-
May 6th, 2004, 09:07 AM
#10
Character strings in C and in C++ are ASCII characters followed
with a null terminator. This is how character strings are defined.
Most developers will assume that you mean character strings
whenever you mention strings.
CString is a C++ class used to manage these types of strings.
std::string is another.
If you want something other than this, you should use CByteArray
or std::array or std::vector. These work with binary data that
isn't seen as a character string.
You can represent binary data in a string - however, you must
convert the binary data to the string format. You can use
CString::Format to do this.
Ex: 0x0 becomes "0" (0x30 0x00 - ASCII '0' followed by null terminator)
1234 becomes "1234" (0x31 0x32 0x33 0x34 0x00 - 4 ASCII
chars followed by null terminator)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|