1

I want to concatenate a char[] with a uint32_t to get another char[] as result:

char word[] = "hi";
uint32_t = 33;

char result[] = "hi33"; //How can I get this ???

The goal is to assemble some data that can be passed to a WriteData method described here with the following signature...

bool WriteData(char *buffer, unsigned int nbChar);
Frank Boyne
  • 4,400
  • 23
  • 30
  • 3
    Use `std::stringstream`. Don't use character arrays. – Thomas Matthews Mar 07 '17 at 01:00
  • `uint32_t = 33;` is a syntax error – M.M Mar 07 '17 at 01:02
  • If you must use a `char[]`, you'll need [`sprintf`](http://www.cplusplus.com/reference/cstdio/sprintf/) – smead Mar 07 '17 at 01:04
  • Hi, I must use char[] because I am interested in the function WriteData of "SerialClass.h", used to write in serial port of arduino: http://playground.arduino.cc/Interfacing/CPPWindows –  Mar 07 '17 at 01:10
  • 2
    Oh boy, look for a better library. That function should take a **`const`** `char*`. Which is easily returned from `std::string::c_str()`. – IInspectable Mar 07 '17 at 01:13
  • I am going to try with sprintf as **smead** said. I let you know when I finish. Thanks for your answers :) –  Mar 07 '17 at 01:15
  • it must be doing division by 10, getting modulus 10, subtracting '0' char value from it(then saving), repeating until last digit is computed. – huseyin tugrul buyukisik Mar 07 '17 at 01:23
  • @Sergio Looking at the definition of `WriteData` it passes the buffer parameter as the second argument to a [WriteFile](https://msdn.microsoft.com/en-us/library/windows/desktop/aa365747.aspx) call. The second parameter to `WriteFile` is a pointer to constant data (`LPCVOID`). As @iinspectable points out WriteData should take `const char *` but since it doesn't you could use `const_cast<>` to remove the `const`ness of `c_str()` – Frank Boyne Mar 07 '17 at 01:53

5 Answers5

5

You would do:

std::string word = "hi";
uint32_t num = 33;
std::string result = word + std::to_string(num);

(without the std:: if you have using namespace std)

Ideally you would not use char[] because there's no reason to.

If necessary a string can be constructed from a char[]

char word[] = "hi";
std::string result {word};

Removing the const qualifier can be dangerous but if a method such as WriteData is known to be safe (i.e., does not modify the contents of its parameter) then it can be called like this

WriteData(const_cast<char *>(result.c_str()), result.length());
Frank Boyne
  • 4,400
  • 23
  • 30
user253751
  • 57,427
  • 7
  • 48
  • 90
2

You can use std::string in this case, but when you go for char [] you can use:

sprintf( result, "%s%u", word, num );

In the case of using string you have several ways: The first way is:

auto result = word + std::to_string(num);

The second way can be using stringstream:

ostringstream oss;
oss << word << num;

and result could be:

oss.str();
Sam Mokari
  • 461
  • 3
  • 11
1

Use std::ostringstream:

static const char word[] = "Elephant";
std::ostringstream stream;
stream << word << 33;
std::string text;
text = stream.str();
Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154
1

One alternative is:

std::string word = "hi";   
std::uint32_t num = 33;

auto result = word + std::to_string(num);
M.M
  • 138,810
  • 21
  • 208
  • 365
1

To write in the serial port of Arduino using that library you should do this:

int _tmain(int argc, _TCHAR* argv[]) {
    Serial* SP = new Serial("COM3");
    HDC dc = GetDC(NULL);
    char word[25];
    uint8_t n;
    uint32_t r, g, b;
    while (SP->IsConnected()) {
        n = sprintf_s(word,"%dr%dg%db\n",r,g,b);
        SP->WriteData(word, n-1);
    }
    return 0;
}

sprintf_s returns the length of the buffer you want to write, yo can use this as the parameter of the WriteData function.

Sergio
  • 844
  • 2
  • 9
  • 26