0

I have an array of unsigned chars:

unsigned char buffer[BUFFER_SIZE];

I want to read in the first N unsigned chars from that array into a string however after looking at strncpy(), that seems to only take pointers to signed chars.

Should I be looking at memcpy()?

Patrick
  • 63
  • 3
  • 10
  • What is the format of the buffer? Is all full or half empty? All values are have the same size? **What have you tryied**? – PaperBirdMaster Oct 16 '12 at 07:06
  • @PaperBirdMaster- Bad Comment man. Doesn't makes sense to me. The question is simple and self-explanatory. – Abhineet Oct 16 '12 at 07:26

3 Answers3

1

Not sure about the exact syntax but, if possible, you should use:

reinterpret_cast<char *>(buffer[i]);

Also see:

Is there a good way to convert from unsigned char* to char*?

Community
  • 1
  • 1
jt234
  • 662
  • 6
  • 16
0

strcmp and memcmp compare data, they don't copy it...

Anyway, just cast to char * and use whatever stdlib function makes sense.

zmbq
  • 38,013
  • 14
  • 101
  • 171
0

If by string you mean a char array, the simplest way in c++ is just to use std::copy, no need to cast anything:

unsigned char buffer[BUFFER_SIZE];
char dest[BUFFER_SIZE]

std::copy( buffer, buffer + N, dest );

If you mean a std::string, just use the iterator style constructor:

std::string dest( buffer, buffer + N );
Ian
  • 183
  • 1
  • 7