0

Intro: Consider that I have no experience with C++, but can find my way around any language to do basic tasks. Most of my experience is with .net languages (vn.net and C#), php and js, so please explain as if you were teaching a complete noob.

Here is the problem: I receive data in an unsigned char characters[12]; and I have to then append that data to a std::string

data is always 12 characters long, and is not null terminated, because it is gathered by selecting specific bytes received from serial port.

Long story short:

std::string stroutput = "some text ";
unsigned char characters[12];
characters[0]=64; //demo data to shorten the code
characters[1]=72;
stroutput.append(characters[0]);
stroutput.append(characters[1]);
....

The result i want is to have the string value

some text @H...

But what i get is an error while compiling

error: invalid conversion from ‘unsigned char’ to ‘const char*’ [-fpermissive]
stroutput.append(characters[0]);
                 ~~~~~~~~~~~~^

Logic behind this thinking is that output of:

printf("%c%c",characters[0],characters[1]);

and

std::cout << characters[0] << characters[0] << std::endl;

results in

@H

So: How do I convert unsigned char something[12] to std::string?

Thanks

Edit: Sorry for the duplicate, search didn't return that question. Thanks everyone - too bad I can't mark multiple answers!

Tomislav Plečko
  • 167
  • 2
  • 14

4 Answers4

1

std::string has a constructorr that takes two iterators:

stroutput += std::string(characters, characters + 12);
bolov
  • 72,283
  • 15
  • 145
  • 224
  • This should be the correct answer especially if buffer is declared as `unsigned char characters[12];`. – Potion Aug 14 '20 at 18:20
1

To append a character you have to use the following overloaded method append

basic_string& append(size_type n, charT c);

For example

stroutput.append(1, characters[0]);
stroutput.append(1, characters[1]);

Otherwise use method push_back or the operator +=. For example

stroutput += characters[0];
stroutput += characters[1];

If you want to append the whole array or its part then you can write

stroutput.append( reinterpret_cast<char *>( characters ), sizeof( characters ) );

stroutput.append( reinterpret_cast<char *>( characters ), n );

where n is the number of characters of the array to append.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

You almost had it, just use string::push_back instead of string::append - append is for concatenating strings, whereas push_back is for adding characters (and deals with resizing the internals etc).

Using a string constructor is normally a better option but here could cause issues as your array isn't null-terminated (see here).

Edit: Actually, constructor 5 from the link above (from-sequence) should be suitable (if you're willing to cast from unsigned char * to const char *, which is safe according to this question+answer):

std::string stroutput(reinterpret_cast<const char *>(characters), 12);
hnefatl
  • 5,860
  • 2
  • 27
  • 49
0

In C++98, the method std::string::append is overloaded with these arguments:

(1) string& append (const string& str);
(2) string& append (const string& str, size_t subpos, size_t sublen);
(3) string& append (const char* s);
(4) string& append (const char* s, size_t n);
(5) string& append (size_t n, char c);
(6) template <class InputIterator> string& append (InputIterator first, InputIterator last);

And in C++11 and C++14, it is also overloaded with string& append (initializer_list<char> il);.

As you can see, there is no candidate for string& append(unsigned char). The error might come from the compiler failing in overload resolution, trying to use the (3) which is for a null-terminated string argument, but fails to find a conversion defined for unsigned char -> const char *.

If you still want to use the method std::string::append, you can use (5): string& append (size_t n, char c) which will append your string with c n times. In your case, it would be:

stroutput.append(1, characters[0]);
stroutput.append(1, characters[1]);

The unsgined char would be converted into char.

Another alternative is to use this constructor for std::string:

std::string(characters, characters + 12);
Muhammad Nizami
  • 904
  • 1
  • 5
  • 9