如何确定 unsigned char* 的长度?

发布于 2024-07-18 20:00:35 字数 31 浏览 8 评论 0原文

如何确定 unsigned char* 的长度?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

澉约 2024-07-25 20:00:35

对于指针的实际大小:

size_t s = sizeof(unsigned char*);

如果想要字符串的长度:

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

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);
稍尽春風 2024-07-25 20:00:35

在理想的世界里,你不会。 您可以将 char* 用于 C 风格字符串(以 NUL 结尾,并且可以测量其长度),而 unsigned char* 仅用于字节数据(其长度在另一个参数或其他参数中,您可能会遇到这种情况)尽快创建 STL 容器,例如 vectorbasic_string)。

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

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

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

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*.

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.

白色秋天 2024-07-25 20:00:35

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

size_t size = sizeof(unsigned char*);

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

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

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.

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.

梦与时光遇 2024-07-25 20:00:35

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

我假设您正在对所述数组进行操作以使其大小不恒定。 如果您只是分配、设置和忘记,您始终可以将数组的实际分配大小存储在单独的变量中 - 或者更好,创建一个结构/类。

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

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

如果比这更复杂,你就需要重新发明 std::string。

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.

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.

念三年u 2024-07-25 20:00:35

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

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

生活了然无味 2024-07-25 20:00:35

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

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.

清醇 2024-07-25 20:00:35

unsigned char s[] = "样本字符串";

int len = sizeof(s);

unsigned char s[] = "sample string";

int len = sizeof(s);

自此以后,行同陌路 2024-07-25 20:00:35

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

strlen(your_string_pointer)

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

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

strlen(your_string_pointer)

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文