简体   繁体   English

如何将byte []复制到char *中?

[英]How to copy a byte[] into a char*?

All I need this for is strcpy(). 我需要的只是strcpy()。

I want to see whether the first three bytes of a buffer(byte array) are "JMX" as string. 我想查看缓冲区(字节数组)的前三个字节是否为字符串的“ JMX”。

This is what I did 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 get exception at strcmp() line. 我在strcmp()行遇到异常。 What is the problem? 问题是什么?

I wish I was writing this in C# :( 我希望我是用C#编写的:(

Tho things go wrong here: 在这里出问题了:

  1. ddj does not point to any actual memory. ddj不指向任何实际内存。 Hence the copy will have undefined behavior 因此,副本将具有未定义的行为
  2. The copying is not necessary 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 , thus ensuring that no more than three bytes are compared. 这使用strncmp而不是strcmp ,从而确保比较的字节数不超过三个。 If buffer can contain less than three bytes, you should do something like: 如果buffer可以包含少于三个字节,则应执行以下操作:

if(buf_len >= 3 && strncmp("JMX", buffer, 3) == 0) {
  buffer += 20;
  size -= 20;
}

You're not allocating any memory for ddj . 您没有为ddj分配任何内存。 Since it's a local variable, it's allocated on the stack. 由于它是一个局部变量,因此它是在堆栈上分配的。 Local variables are not initialized to 0/false/NULL by default, so the value of ddj immediately after it's declared is undefined -- it will have the value of whatever is left in memory at that particular location on the stack. 默认情况下,局部变量未初始化为0 / false / NULL,因此ddj声明后的值是未定义的-它的值将保留在堆栈上特定位置的内存中。 Any attempt to dereference it (that is, to read or write the memory at which it's pointing) will have undefined behavior. 任何取消引用它的尝试(即读取或写入它所指向的内存)都将具有未定义的行为。 In your case, it's crashing because it's pointing to an invalid address. 在您的情况下,它崩溃是因为它指向一个无效的地址。

To fix the problem, you need to allocate storage for ddj . 要解决此问题,您需要为ddj分配存储。 You can either allocate static storage on the stack, or dynamic storage on the heap. 您可以在堆栈上分配静态存储,也可以在堆上分配动态存储。 To allocate static storage, do: 要分配静态存储,请执行以下操作:

// 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 storage yourself, it would be a better idea to use std::string , which automatically deals with memory management. 与其亲自管理存储,不如使用std::string ,它会自动处理内存管理,这是一个更好的主意。

Finally, since all you're doing is comparing the first three characters of the string, there's no need to jump through hoop to copy the string and compare it. 最后,由于您要做的只是比较字符串的前三个字符,因此无需跳过箍来复制字符串并进行比较。 Just use strncmp() : 只需使用strncmp()

if(strncmp(buffer, "JMX", 3) == 0)
{
    ...
}

You have not allocated memory for ddj. 您尚未为ddj分配内存。 Allocate memory using new to it . 使用new分配内存。 For example 例如

char *ddj = new char[size]; //Allocate size number of chars
//do the required comaprisons

delete[] ddj; //Remember to release the memory.

On the other hand you can use std::string which is a standard string class. 另一方面,您可以使用std :: string这是一个标准的字符串类。

You must allocate new memory for ddj . 您必须为ddj分配新的内存。 Either 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 . 一个更方便的替代方法是std::string

Firstly, it's crashing because ddj doesn't point to anything. 首先,它崩溃是因为ddj没有指向任何东西。

Secondly, you don't need to copy the data from byte[] to char* (they're essentially the same thing). 其次,您不需要将数据从byte []复制到char *(它们本质上是相同的)。 You can just do: 您可以这样做:

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

This is UB because ddj isn't pointing to anything. 这是UB,因为ddj没有指向任何东西。 You need to allocate memory: 您需要分配内存:

char* ddj = new char[strlen(buffer) + 1];

Be sure to delete the memory you allocated using delete[] (not plain delete !). 一定要delete您使用分配的内存delete[]非纯delete !)。

You could also use std::string which is generally safe, as you don't have to deal with pointers and memory alloation. 您也可以使用std::string ,这通常是安全的,因为您不必处理指针和内存分配。

Looking at your code, however, ddj seems useless. 然而, ddj您的代码, ddj似乎没用。 Just use buffer : 只需使用buffer

if ( strcmp( "JMX", buffer ) == 0 ) //check first three chars are "JMX"
{
    buffer += 20;   //increase the index with 20
    size -= 20;     //int
}

如果要strcmp ddj,也可以先在缓冲区上进行操作,如果以后需要,可以制作缓冲区的副本。

You are getting the exception because the variable 'ddj' isn't initialized. 由于未初始化变量'ddj',因此您正在获取异常。 It is pointing at garbage, so who knows where you are copying that string... 它指向垃圾,所以谁知道您要将字符串复制到哪里...

You don't really need to copy the bytes before comparing them, though. 不过,在比较它们之前,您实际上并不需要复制字节。

if(strncmp("JMX", buffer, 3) == 0) // check if the first three characters are "JMX"
{
    buffer += 20;
    size -= 20;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM