unsigned char * to string

D

daniel.doyle

Possibly a dumb question, but my c++ experience isn't that great.

I have a method:

static XMLRequest *parseXMLRequest(unsigned char *byte_buffer, size_t
length)
{}

byte_buffer is a pointer to a unsigned char array, but inside the
method I need to use it as a string to pass to another method. Can
anyone tell me the easiest/best way to convert? I've tried a couple of
things I thought might work, but I've had no luck so far.

Thanks
 
V

Victor Bazarov

Possibly a dumb question, but my c++ experience isn't that great.

I have a method:

static XMLRequest *parseXMLRequest(unsigned char *byte_buffer, size_t
length)
{}

byte_buffer is a pointer to a unsigned char array, but inside the
method I need to use it as a string to pass to another method. Can
anyone tell me the easiest/best way to convert? I've tried a couple of
things I thought might work, but I've had no luck so far.

Just cast it to (char*). What problems are you encountering?

V
 
D

Dan

I was getting "could not be converted errors" I hadn't even thought of
casting. It seems to work now, thanks!

But can you tell me what happens when I cast? Is it making a copy of
the variable?
 
F

Frederick Gotham

posted:
Possibly a dumb question, but my c++ experience isn't that great.

I have a method:

static XMLRequest *parseXMLRequest(unsigned char *byte_buffer, size_t
length)
{}

byte_buffer is a pointer to a unsigned char array, but inside the
method I need to use it as a string to pass to another method. Can
anyone tell me the easiest/best way to convert?


#include <cstddef>

class SomeClass {
public:

class XMLRequest {};


static XMLRequest *parseXMLRequest(unsigned char*,std::size_t)
{
static XMLRequest req;

return &req;
}
};


int main()
{
char buffer[60];

SomeClass().parseXMLRequest(reinterpret_cast<unsigned char*>(&*buffer),
sizeof buffer );
}
 
R

red floyd

Dan said:
> I was getting "could not be converted errors" I hadn't even thought of
> casting. It seems to work now, thanks!
>
> But can you tell me what happens when I cast? Is it making a copy of
> the variable?

1. Please do not top-post. Rearranged to conform with comp.lang.c++
netiquette.

2. The cast doesn't make a copy, it tells the compiler "Please use this
unsigned char * as a char * instead, and yes, I know what I'm doing."
Whether or not you know what you're doing is up to you and your code.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top