0
LPBYTE buffer = (LPBYTE)calloc(1024, sizeof(char));
std::string res;

I want something like:

res = buffer;
João Paulo
  • 6,300
  • 4
  • 51
  • 80
sandro98
  • 29
  • 6
  • Possible duplicate of [Copying non null-terminated unsigned char array to std::string](https://stackoverflow.com/questions/4691608/copying-non-null-terminated-unsigned-char-array-to-stdstring) – Axalo Sep 16 '19 at 14:04
  • What do you want to achieve? Are you looking for `std::string` to own the buffer? Or to hold a copy of its content? – StoryTeller - Unslander Monica Sep 16 '19 at 14:24

1 Answers1

1

You can use the std::string constructor (number 6 in the link) that uses iterators to copy the buffer into a string:

std::string res(buffer, buffer + 1024);

Note that there is no conversion other than the unsigned chars in your buffer being converted to chars in the std::string.

Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108