CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Thread: String Copy

  1. #1
    Join Date
    May 2003
    Posts
    347

    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

  2. #2
    Join Date
    Nov 2002
    Location
    Devon, UK
    Posts
    212
    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.

  3. #3
    Join Date
    May 2003
    Posts
    347
    Hello

    THanks for the reply. I want "a0b" in that CString variable. Thats what the requirement is . THis is not a unicde build

    Thanks

  4. #4
    Join Date
    Nov 2002
    Location
    Devon, UK
    Posts
    212

    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.

  5. #5
    Join Date
    May 2003
    Posts
    347
    No ,
    just take one example. a[3] = {'a',0,'b'}


    This I want to copy to a string

    and string shud give me "a0b"

  6. #6
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430
    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
    Code:
    char a[4] = "a0b";

  7. #7
    Join Date
    May 2003
    Posts
    347
    ok Sir

    I got it

    Thanks everybody

    it was a mistake from my side

    chao

  8. #8
    Join Date
    Jun 2002
    Posts
    395
    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.

  9. #9
    Join Date
    Nov 2002
    Location
    Devon, UK
    Posts
    212
    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.

  10. #10
    Join Date
    Aug 2001
    Location
    Texas
    Posts
    645
    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
  •  





Click Here to Expand Forum to Full Width

Featured