c++ string as c string

This is a discussion on c++ string as c string within the C++ Programming forums, part of the General Programming Boards category; Hello.. Im working on mx dns resolver and I'm making a header that has to be sent to nameserver.. Normally ...

  1. #1
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630

    c++ string as c string

    Hello..

    Im working on mx dns resolver and I'm making a header that has to be sent to nameserver..
    Normally I would make the header with c string that way (or copy the packed struct to char):
    unsigned char dns[512] = {1,1, 1,0, 0,1, 0,0, 0,0, 0,0};

    How should I do this with c++ string? What would be the proper way?

    Thanks

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,074
    Code:
    std::string dns = "111001000000";
    
    dns.c_str(); // Returns a (const?) char* to the string in the object
    You want something like this, I suppose? If you're in a situation where you have to use a union and struct to send and receive this header without conversion errors, then you may be better off using an array. I'm not sure a string object directly reflects the size of the string it holds. I wouldn't quote me on that, though. It can be tricky introducing C++ into network libraries that are written for C.
    Sent from my iPadŽ

  3. #3
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    If you know the size of the array, and are not going to be manipulating it directly, then what you have:

    Code:
    unsigned char dns[512] = {1,1, 1,0, 0,1, 0,0, 0,0, 0,0};
    Is fine, no need to go after STL just to be a hit up every language feature. If you need to concatenate a bunch of stuff into some sort of container, you could do some funny stuff with a vector I guess:

    Code:
    unsigned char dns[512] = {1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0}; // does it need to be 512?
    unsigned char moredata[512] = { 0 }; // more data you need to build into something?
    std::vector<unsigned char> v;
    
    v.resize(sizeof dns);
    std::copy(dns, dns + sizeof dns, v.begin());
    
    v.resize(v.size() + sizeof moredata);
    std::copy(moredata, moredata + sizeof moredata, v.begin() + sizeof dns);
    Or something.

    Code:
    std::string dns = "111001000000";
    
    dns.c_str(); // Returns a (const?) char* to the string in the object
    You want something like this, I suppose? If you're in a situation where you have to use a union and struct to send and receive this header without conversion errors, then you may be better off using an array. I'm not sure a string object directly reflects the size of the string it holds. I wouldn't quote me on that, though. It can be tricky introducing C++ into network libraries that are written for C.
    That string will contain ascii values, and stuff. His data is not null-terminated and not really meant to be represented by a string.

    ╔╗╔╦══╦╗╔╦══╦╗
    ║╚╝║╔╗║╚╝║╔╗║║
    ║╔╗║╠╣║╔╗║╠╣╠╣
    ╚╝╚╩╝╚╩╝╚╩╝╚╩╝

    codez http://code.google.com/p/zxcvbn/

  4. #4
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630
    Lets say I have a packed structure header (struct dnsheader) filled with data..

    Code:
    dnsheader *header = makeheader();
    char *query = new char[512];
    
    memset(query, 0, 512);
    memcpy(query, header, sizeof(dnsheader));
    How should I copy struct to c++ string? What would be the proper way to do this?

  5. #5
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Code:
    dnsheader *header = makeheader();
    
    std::string query( (char *) header, sizeof(dnsheader) );
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  6. #6
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630
    Quote Originally Posted by Cat
    Code:
    dnsheader *header = makeheader();
    
    std::string query( (char *) header, sizeof(dnsheader) );
    This wont work.. I get error..

    What about something like:

    string query += header;

    that wouldnt be okay, but what would be?
    Would it be better to use vector as Tonto said, since my data wont be null-terminated.. And It wont contain only header..
    Last edited by l2u; 09-22-2006 at 08:40 AM.

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >This wont work.. I get error..
    Post what error you're getting.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. compare structures
    By lazyme in forum C++ Programming
    Replies: 15
    Last Post: 05-28-2009, 03:40 AM
  2. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 05:30 PM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 07:49 PM

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