Thread: c++ string vs. c string

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    630

    c++ string vs. c string

    Hello guys

    I have a dns reply parser, which is written in c code.. The reply is in binary, so parsing data is a little different as normally.. I wonder if would it be easier/better to use c++ string instead of char and char pointers for processing data?

    Im used to char *p = sth; sth++; etc..

    Heres the example of a function so you get a better idea what I'm doing..

    Code:
    int decodehostname(char *buf, char *packet, unsigned int len, unsigned int pos) {
    	unsigned int label;
    	int bufpos = 0, r = 0;
    
    restart:
    	while(pos < len) {
    		label = packet[pos++];
    
    		if (label & 0xC0) {
    			if (pos >= len) return 0;
    			label = ((label & 0x3F) << 8) | packet[pos++];
    
    			if (r == 0) r = pos;
    			pos = label;
    
    			goto restart;
    		}
    		//printf("   bufpos: %d pos: %d label: %d\n   len: %d\n", bufpos, pos, label, len);
    		if (label == 0)
    			break;
    
    		//check
    		if (bufpos + label + 1 >= 128) return 0;
    		if (pos + label > len) return 0;
    
    		memcpy(buf + bufpos, packet + pos, label);
    		pos += label;
    		bufpos += label;
    		buf[bufpos++] = '.';
    	}
    	if (bufpos) bufpos--;
    	buf[bufpos] = 0;
    
    	return (r ? r : pos);
    }
    Thanks for help again

  2. #2
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    Code:
    int decodehostname(string &buf, string &packet, unsigned int len, unsigned int pos) {
          unsigned int label;
          int bufpos = 0, r = 0;
    
          while(pos < len) {
                label = packet[pos++];
    
                if (label & 0xC0) {
                      if (pos >= len) return 0;
                      label = ((label & 0x3F) << 8) | packet[pos++];
    
                      if (r == 0) r = pos;
                      pos = label;
    
                      continue;
                }
                //printf("   bufpos: %d pos: %d label: %d\n   len: %d\n", bufpos, pos, label, len);
                if (label == 0)
                      break;
    
                //check
                if (bufpos + label + 1 >= 128) return 0;
                if (pos + label > len) return 0;
    
                string part = packet.substr(pos, label);
                buf.replace(bufpos, part.length() ,part );
                pos += label;
                bufpos += label;
                buf[bufpos++] = '.';
          }
          if (bufpos) bufpos--;
          buf[bufpos] = 0;
    
          return (r ? r : pos);
    }
    Last edited by siavoshkc; 09-15-2006 at 10:20 AM.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    It's usually easier to use the C++ string class than C strings. They can do everything C arrays can, and if you need a C string you can use the .c_str() member function (which returns a const char *; if you need a modifiable C string then you need to use one).
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

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, 02:40 AM
  2. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04: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, 06:49 PM