whole char array into string as int

This is a discussion on whole char array into string as int within the C++ Programming forums, part of the General Programming Boards category; hi, I'm wanting to find out a way to display/convert a whole char array or string into int, such that ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Location
    Australia
    Posts
    55

    whole char array into string as int

    hi,

    I'm wanting to find out a way to display/convert a whole char array or string into int, such that their hex values are displayed & not characters. I know how to do this, but I'm wanting to find out if there's a way to do it in one pass & not byte by byte

    For example, the following does it one at a time, which is fine for small strings, but not for large data blocks.

    Code:
    char Temp[32];
    unsigned char* pbuffer = somedata;
    string str;
    	for(int i=0; i< Charsize;i++)
    	{
    	        sprintf(Temp,"%02x ",pbuffer[i]);
    		str +=Temp;
    	}

    I'm after more something like this:

    Code:
    unsigned char *pbuffer = "The quick brown fox jumps over the lazy dog";
    std::string szStringConvert2Int(pbuffer);
    Where this would convert & store int values in one go into szString or something like along these lines.

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,761
    Quote Originally Posted by Tropod View Post
    I'm after more something like this:

    Code:
    unsigned char *pbuffer = "The quick brown fox jumps over the lazy dog";
    std::string szStringConvert2Int(pbuffer);
    Where this would convert & store int values in one go into szString or something like along these lines.
    What you are after looks like a constructor for std::string that will do this. There isn't a standard one like that, and I don't think writing a whole class is necessary, just write a stringConvert2Int function in a similar vein to what you already have.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Why is it not fine for large blocks?
    IIRC, those std::string operator+=() calls in there are amortised constant time just like for a vector.

    I would use a stringstream myself, casting each unsigned char to an int and using the std::hex modifier on the stream. But I would not expect it to be a lot faster than the above. Not that I would expect anythign else to be a lot faster either.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Convert string to char or use 2d array of char?
    By simpleblue in forum C++ Programming
    Replies: 6
    Last Post: 09-25-2011, 06:00 PM
  2. Array Char to String with \0
    By sergioms in forum C Programming
    Replies: 10
    Last Post: 07-23-2010, 05:52 AM
  3. convert char** (c string array) to std::string[]
    By umen242 in forum C++ Programming
    Replies: 2
    Last Post: 11-11-2008, 05:52 AM
  4. Appending char to string (char array)
    By sniper83 in forum C Programming
    Replies: 14
    Last Post: 04-15-2008, 07:48 AM
  5. String to Char array?
    By Justin C in forum C++ Programming
    Replies: 1
    Last Post: 04-29-2005, 09:00 PM

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