Thread: Binary char pointer into C++ string

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

    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;
    }
    Using Windows 10 with Code Blocks and MingW.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    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
    932
    Because I use the function recv() that takes a char*.
    Using Windows 10 with Code Blocks and MingW.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    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

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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);
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    Great, thanks for both of you! :-)
    Using Windows 10 with Code Blocks and MingW.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 12-02-2012, 05: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