6

I have a string defined as std::string header = "00110033"; now I need the string to hold the byte values of the digits as if its constructed like this

char data_bytes[] = { 0, 0, 1, 1, 0, 0, 3, 3};
std::string header = new std::string(data_bytes, 8).c_str());

I converted the initial string to int array using atoi. Now i'm not sure how to make the string out of it. Let me know if there is any better approach.

4
  • 1
    I am unable to understand your question. Commented Mar 7, 2011 at 17:41
  • syntax of 2nd line isn't correct. Also, what's the intention of the new here? Commented Mar 7, 2011 at 17:42
  • Why do you need to do this? What are you going to do if the array contains a value that is not between 0 and 9? Commented Mar 7, 2011 at 18:03
  • It's inefficient (due to copying) and a memory leak (due to new without storing the pointer for a corresponding delete) to say std::string header = (new std::string(data_bytes, 8))->c_str(); (or whatever it is you meant). You can just say std::string header(data_bytes, 8); instead. Commented Mar 7, 2011 at 18:18

6 Answers 6

4

you could write a little function

string int_array_to_string(int int_array[], int size_of_array) {
  string returnstring = "";
  for (int temp = 0; temp < size_of_array; temp++)
    returnstring += itoa(int_array[temp]);
  return returnstring;
}

untested!

a slightly different approach

string int_array_to_string(int int_array[], int size_of_array) {
  ostringstream oss("");
  for (int temp = 0; temp < size_of_array; temp++)
    oss << int_array[temp];
  return oss.str();
}
Sign up to request clarification or add additional context in comments.

3 Comments

@ultifinitus atoi converts string to int, while you need to convert int to string. You need to use itoa, which is not standard function, so probably best solution is to convert int to character by adding '0' (see my solution). You also can use lexical cast: boost::lexical_cast<std::string>(int_array[temp]);
Use std::stringstream instead, it's safer.
Oh good heavens, I'll revamp it, it was a 5 second function geez! =)
2

Do this:

  char data_bytes[] = { '0', '0', '1', '1', '0', '0', '3', '3', '\0'};
  std::string header(data_bytes, 8);

Or maybe, you want to do this:

  std::stringstream s;
  s << data_bytes;
  std::string header = s.str();

Demo at ideone : http://ideone.com/RzrYY


EDIT:

Last \0 in data_bytes is necessary. Also see this interesting output here: http://ideone.com/aYtlL

PS: I didn't know this before, thanks to Ashot I came to know this difference by experimenting!

11 Comments

You can't store ints in a char array
@Nawaz -1 data_bytes is not ending with '\0' so you can't print it. + In both cases your string will not be equal to string("00110033");
You won't get the results you expect
@Ashot: I saw that, and fixed it as well. @Nicklamort: The problem is fixed. However, it was nothing to do with "storing ints ina char".
@Nawaz remove last '\0' in data_bytes. Your string is ending with double '\0' now :) See this topic stackoverflow.com/questions/4711449/….
|
1
 char data_bytes[] = { 0, 0, 1, 1, 0, 0, 3, 3};
  std::string str;
 for(int i =0;i<sizeof(data_bytes);++i)
      str.push_back('0'+data_bytes[i]);

Comments

1

Assuming you're using a "fairly normal" system where the numeric values of '0' to '9' are consecutive, you can just iterate over each element and subtract '0':

for(int i = 0; i < header.size(); ++i)
{
    header[i] -= '0';
}

2 Comments

Shouldn't you be adding instead of substracting?
@Eugen Constantin Dinca The OP has a string like "00111" and wants it to be translated to one with those actual byte values, for example "00111" -> "\000\000\001\001\001". Thus you subtract.
1

You can do this:

std::string header( data_bytes, data_bytes + sizeof( data_bytes ) );
std::transform( header.begin(), header.end(), header.begin(), 
     std::bind1st( std::plus< char >(), '0' ) );

Comments

0

If integ[] is the integer array, and s is the final string we wish to obtain,

string s="";

for(auto i=0;i<integ.size()-1; ++i)
    s += to_string(ans[i]); 

cout<<s<<endl;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.