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
    856

    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
    3,268
    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
    856
    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
    3,268
    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
    23,639
    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.
    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
    856
    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