How to copy byte [] to char *?
All I need is strcpy ().
I want to see if the first three bytes of the buffer (byte array) are "JMX" as a string.
This is what I have done so far:
char * ddj;
strcpy( ddj, buffer ); //buffer is BYTE[]
if ( strcmp( "JMX", ddj ) == 0 ) //check first three chars are "JMX"
{
buffer += 20; //increase the index with 20
size -= 20; //int
}
I am getting an exception on the strcmp () line. What is the problem?
I wish I had written this in C # :(
a source to share
This is where things go wrong:
-
ddj
does not indicate any real memory. Hence the copy will have undefined behavior - Copying is not required in the first place.
This is what you can do:
if(strncmp("JMX", buffer, 3) == 0) {
buffer += 20;
size -= 20;
}
This uses strncmp instead of strcmp , which ensures that no more than three bytes are compared. If it buffer
can contain less than three bytes, you should do something like:
if(buf_len >= 3 && strncmp("JMX", buffer, 3) == 0) {
buffer += 20;
size -= 20;
}
a source to share
You are not allocating any memory for ddj
. Since this is a local variable, it is allocated on the stack. Local variables are not initialized by default 0 / false / NULL, so the value ddj
immediately after the declaration is undefined - it will have the value of what's left in memory at that particular place on the stack. Any attempt to dereference it (that is, read or write the memory it points to) will have undefined behavior. In your case, this is a failure because it points to the wrong address.
To fix the problem, you need to allocate storage for ddj
. You can either allocate static storage on the stack or dynamic storage on the heap. To allocate static storage, follow these steps:
// Allocate 64 bytes for ddj. It will automatically be deallocated when the function
// returns. Be careful of buffer overflows!
char ddj[64];
To allocate dynamic storage:
// Allocate 64 bytes for ddj. It will NOT be automatically deallocated -- you must
// explicitly deallocate it yourself at some point in the future when you're done
// with it. Be careful of buffer overflows!
char *ddj = new char[64];
...
delete [] ddj; // Deallocate it
Instead of managing the storage yourself, it would be better to use std::string
one that handles memory management automatically.
Finally, since all you do is compare the first three characters of the string, there is no need to jump over the hoop to copy the string and compare it. Just use strncmp()
:
if(strncmp(buffer, "JMX", 3) == 0)
{
...
}
a source to share
You haven't allocated memory for ddj. Allocate memory using a new one for it. for instance
char *ddj = new char[size]; //Allocate size number of chars
//do the required comaprisons
delete[] ddj; //Remember to release the memory.
Alternatively, you can use std :: string, which is the standard string class.
a source to share
This is UB because it ddj
doesn't indicate anything. You need to allocate memory:
char* ddj = new char[strlen(buffer) + 1];
Necessarily delete
allocated memory using delete[]
(not easy delete
!).
You can also use std::string
which is generally safe as you don't have to deal with pointers and memory addition.
However, looking at your code ddj
seems gimmicky. Just use buffer
:
if ( strcmp( "JMX", buffer ) == 0 ) //check first three chars are "JMX"
{
buffer += 20; //increase the index with 20
size -= 20; //int
}
a source to share
You are getting an exception because the 'ddj' variable is not initialized. It points to garbage, so who knows where you are copying that line ...
You don't really need to copy bytes before comparing them.
if(strncmp("JMX", buffer, 3) == 0) // check if the first three characters are "JMX"
{
buffer += 20;
size -= 20;
}
a source to share