C++ 你如何确定一个无符号字符*的长度?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/836549/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 17:30:28  来源:igfitidea点击:

How do you determine the length of an unsigned char*?

c++ccharunsigned

提问by joce

How do you determine the length of an unsigned char*?

你如何确定一个无符号字符*的长度?

回答by joce

For the actual size of the pointer:

对于指针的实际大小:

size_t s = sizeof(unsigned char*);

If you want the length of the string:

如果你想要字符串的长度:

unsigned char* bla = (unsigned char*)"blabla";
int s = strlen((char*)bla);

回答by Steve Jessop

In an ideal world, you don't. You use char* for C-style strings (which are NUL-terminated and you can measure the length of), and unsigned char* only for byte data (which comes with its length in another parameter or whatever, and which you probably get into an STL container ASAP, such as vector<unsigned char>or basic_string<unsigned char>).

在理想的世界中,您不会。您将 char* 用于 C 样式字符串(它们以 NUL 结尾,您可以测量其长度),而 unsigned char* 仅用于字节数据(它的长度在另一个参数或其他参数中,您可能会遇到尽快使用 STL 容器,例如vector<unsigned char>basic_string<unsigned char>)。

The root problem is that you can't make portable assumptions about whether the storage representations of char and unsigned char are the same. They usually are, but they're allowed not to be. So there are no string-like library functions which operate on unsigned char*, only on char*, and it is not in general safe to cast unsigned char* to signed char* and treat the result as a string. Since char might be signed, this means no casting unsigned char* to char*.

根本问题是您无法对 char 和 unsigned char 的存储表示是否相同做出可移植的假设。他们通常是,但他们被允许不是。因此,没有类似字符串的库函数可以对 unsigned char* 进行操作,仅对 char* 进行操作,并且将 unsigned char* 转换为有符号 char* 并将结果视为字符串通常是不安全的。由于 char 可能是有符号的,这意味着不会将 unsigned char* 转换为 char*。

However, 0 is always the same value representation in unsigned char and char. So in a non-ideal world, if you've got a C-style string from somewhere but it has arrived as an unsigned char*, then you (a) cast it to char* and get on with it, but also (b) find out who did this to you, and ask them please to stop.

但是,0 在 unsigned char 和 char 中始终是相同的值表示。所以在一个非理想的世界中,如果你从某个地方得到了一个 C 风格的字符串,但它是作为一个 unsigned char* 到达的,那么你 (a) 将它转换为 char* 并继续使用它,而且 (b ) 找出是谁对你做了这件事,并请他们停下来。

回答by JaredPar

There could be two meanings to this. Are you just wanting to know how big the pointer type is? If so then Joce's answer is correct

这可能有两种含义。你只是想知道指针类型有多大?如果是这样,那么乔斯的答案是正确的

size_t size = sizeof(unsigned char*);

If you're wanting to know how many elements does the pointer point to, that's a bit more complex. If this is a C style string then strlen or some variant is your best option.

如果您想知道指针指向多少个元素,那就有点复杂了。如果这是一个 C 风格的字符串,那么 strlen 或一些变体是你最好的选择。

However if this is just a pointer to unsigned char which has no relation to a C style string, then there is no way to reliably achieve what you're looking for. C / C++ does not associate a length field with a pointer. You'll need to pass the length around with the pointer or use a class like vector which stores both the pointer and the length.

但是,如果这只是一个指向与 C 样式字符串无关的 unsigned char 的指针,则无法可靠地实现您正在寻找的内容。C/C++ 不会将长度字段与指针相关联。您需要将长度与指针一起传递,或者使用类似 vector 的类来存储指针和长度。

回答by cyberconte

If you're using C++, and its a string in an unsigned char*, you're better off first putting it into a std::string before manipulating it. That way you can do all kinds of things to it and still be able to get the length() and/or capacity() of it whenever you want.

如果您使用的是 C++,并且它是一个 unsigned char* 中的字符串,那么在操作它之前最好先将它放入 std::string 中。这样你就可以对它做各种各样的事情,并且仍然可以随时获得它的 length() 和/或 capacity() 。

I'm assuming that you're doing things to said array to make its size non-constant. If you're just allocating, setting, and forgetting, you can always store the actual allocation size of the array in a seperate variable - or better, make a struct/class.

我假设您正在对所述数组进行处理以使其大小非常规。如果你只是分配、设置和忘记,你总是可以将数组的实际分配大小存储在一个单独的变量中——或者更好的是,创建一个结构/类。

//WARNING: memory issues not addressed here.
struct myStringStruct
{
  unsigned char * string;
  int len;

  allocate(int size) {
    len = size;
    string = malloc(sizeof(unsigned char) * len);
  }
}

Any more complex than that and you're re-inventing std::string.

任何比这更复杂的事情,你就会重新发明 std::string。

回答by Ageliver

if you compile in c code, the strlen()function params can deal “unsigned char*”;but in c++ code, the params can't deal "unsigned char*";so if you in c++ code compile need to force translate (unsigned char*)str.

如果你在c代码中编译,strlen()函数params可以处理, “unsigned char*”;但在c++代码中,params不能处理,"unsigned char*";所以如果你在c++代码中编译需要强制翻译(unsigned char*)str.

回答by Rohit

Do you want the length of the pointer, which would be an int. If you want the length of the string that is being pointed to, use strlen: e.g. Size of the pointer: sizeof(unsigned char*) Size of the string: strlen(unsigned char*) Multibyte characters will get reported as ..multi byte

你想要指针的长度吗,这将是一个int。如果您想要被指向的字符串的长度,请使用 strlen:例如指针的大小:sizeof(unsigned char*) 字符串的大小:strlen(unsigned char*) 多字节字符将被报告为 ..multi byte

回答by Coincoin

By unsigned char * I suppose you mean the string located at that pointer. In that case it would be:

通过 unsigned char * 我想你的意思是位于该指针处的字符串。在这种情况下,它将是:

strlen(your_string_pointer)

However, this will only find the \0 position. There is no garantee this is the actual allocated memory block size.

但是,这只会找到 \0 位置。没有保证这是实际分配的内存块大小。