1

I have a function that expects a uint8 * representing a buffer and an int representing the buffer length. How should I go about transforming a std::string into a uint8 * representing the string? I have tried using c_str() to transform it into a char * but I still can't convert to uint8 *.

The function signature looks like.

InputBuffer(const UINT8* buf, int len, bool usesVarLengthStreams = true)

and I am trying to do the following

string ns = "new value";
int len = 10;

UINT8 * cbuff = ns; //(UINT8 *) ns.c_str(); doesn't work

in = new InputBuffer(cbuff, len);
JME
  • 2,293
  • 9
  • 36
  • 56
  • Where does `UINT8` come from? Is it perhaps a `typedef` for some variant of `char`? Also, please describe *how* casting `c_str()` to `UINT8*` "doesn't work". –  May 08 '14 at 20:37
  • the line `new InputBuffer(cbuff, len)` attempts to create an object of type `InputBuffer`. But earlier you said that `InputBuffer` was a function. Please clarify. Also, there is nothing wrong with the line that you said "doesn't work", and you pass a length of 10 where perhaps you should be passing a length of 9. – M.M May 08 '14 at 22:01

1 Answers1

3

string.c_str() returns a (const char *) pointer, while UINT8 is:

typedef unsigned char UINT8; 

So the code should be:

const UINT8 * cbuff = static_cast<const UINT8 *>(ns.c_str());

Matt
  • 6,010
  • 25
  • 36