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 # :(

0


a source to share


8 answers


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;
}

      

+8


a source


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)
{
    ...
}

      

+6


a source


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.

+2


a source


You must allocate new memory for ddj

. Or declare it as

char ddj[NAX_LENGTH];

      

or with dynamic allocation

char* ddj = new char[length]; // You must use delete[] to free the memory in the end.

      

A more convenient alternative is std::string

.

+2


a source


First, it crashes because it ddj

doesn't point to anything.

Second, you don't need to copy data from byte [] to char * (they are essentially the same thing). You can simply:

if (strncmp("JMX", reinterpret_cast<char*>(buffer), 3) == 0)
{
  // Strings are equal, do what you want
}

      

+1


a source


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
}

      

0


a source


if you want strcmp ddj you can also do it in the buffer first and make a copy of the buffer if you need it later.

0


a source


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;
}

      

0


a source







All Articles