Like Tree3Likes
  • 1 Post By jimblumberg
  • 2 Post By laserlight

Binary char pointer into C++ string

This is a discussion on Binary char pointer into C++ string within the C++ Programming forums, part of the General Programming Boards category; Is there a more simple method to copy Buf into str? Buf is a binary string. Code: void function(string & ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    793

    Binary char pointer into C++ string

    Is there a more simple method to copy Buf into str?
    Buf is a binary string.

    Code:
    void function(string & str)
    {
        int iWholeSize = 512;
        char * Buf = new char[iWholeSize];
        .... operations on Buf
        string s(Buf, Buf + iWholeSize);
        str = s;
    }
    Compiler MSVC++ 2013 with Code::Blocks.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    2,753
    Why are you using the character array for Buf? Why not just use a string?

    Jim

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    793
    Because I use the function recv() that takes a char*.
    Compiler MSVC++ 2013 with Code::Blocks.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    2,753
    If you insure Buf is a properly terminated C-string you could just use the assignment operator.
    Code:
    str = Buf;
    But if Buf is not a C-string then using the constructor is best. But your constructor looks incorrect. Shouldn't it be:
    Code:
    string s(Buf, value_returned_from_recv_calls);
    ;
    You could also avoid the temp variable with:
    Code:
    str = string(Buf, value_returned_from_recv_calls);
    Jim
    Ducky likes this.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,717
    Quote Originally Posted by jimblumberg
    You could also avoid the temp variable with:
    Code:
    str = string(Buf, value_returned_from_recv_calls);
    Or better yet:
    Code:
    str.assign(Buf, value_returned_from_recv_calls);
    Ducky and jimblumberg like this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Dec 2007
    Posts
    793
    Great, thanks for both of you! :-)
    Compiler MSVC++ 2013 with Code::Blocks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 12-02-2012, 04:25 AM
  2. Converting a char string into an integer and then into binary
    By Avanish Giri in forum C Programming
    Replies: 13
    Last Post: 04-09-2012, 12:23 AM
  3. Replies: 11
    Last Post: 06-16-2011, 11:59 AM
  4. convert string of binary to Char
    By yagmai in forum C Programming
    Replies: 2
    Last Post: 03-10-2008, 12:37 PM
  5. Pointer to String and Pointer to Char
    By vb.bajpai in forum C Programming
    Replies: 3
    Last Post: 06-15-2007, 03:03 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21