计算嵌入空值的字节字符串的长度 [英] Calculate length of byte string with embedded nulls

查看:91
本文介绍了计算嵌入空值的字节字符串的长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好


我有一个字节流 - unsigned char *。但''字符串''可能包含

嵌入的空值。所以不像传统的c字符串以null结尾。


我需要计算这些数组的长度但是不能使用strlen因为

它只是在它找到的第一个空值处停止计数。怎么做呢?


Angus

解决方案

Angusaécrit:


你好


我有一个字节流 - unsigned char *。但''字符串''可能包含

嵌入的空值。所以不像传统的c字符串以null结尾。


我需要计算这些数组的长度但是不能使用strlen因为

它只是在它找到的第一个空值处停止计数。怎么做呢?


Angus



没有办法,因为你没有算法确定

的长度。


安格斯说:


你好


我有一个字节流 - unsigned char *。但''字符串''可能包含

嵌入的空值。所以不像传统的c字符串以

null结尾。


我需要计算这些数组的长度但是不能使用strlen

因为

它只是在它找到的第一个空值处停止计数。那该怎么办?



嗯,现在你知道null是什么了。 :-)


无论何时读取数据,都需要建立停止协议。如果

你正在阅读一个文本文件,那么当你点击换行符时,通常会停止(或至少暂停

)。如果您正在阅读电子邮件Feed,当您收到.\r\ n时,您将停止

。如果你要复制一个字符串,你就停在null

终止符。所有这些都是终止协议。


显然,你也需要一个终止协议。如果没有特定值(''\ 0'',

''\ n'')或值组合(。\\\\ n)表明自己是哨兵,

那么你别无选择,只能坚持你的数据源是

,并附上有关其长度的相关信息。


-

Richard Heathfield

Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:rjh在上述域名, - www。


Angus写道:


你好


我有一个字节流 - unsigned char *。但''字符串''可能包含

嵌入的空值。所以不像传统的c字符串以null结尾。


我需要计算这些数组的长度但是不能使用strlen因为

它只是在它找到的第一个空值处停止计数。那该怎么办?



如果此流具有特定格式并且长度嵌入在

中,则可以将其提取出来。怎么做取决于格式。

否则,如果长度没有保存在别处,你需要自己跟踪




Hello

I have a stream of bytes - unsigned char*. But the ''string'' may contain
embedded nulls. So not like a traditional c string terminated with a null.

I need to calculate the length of these arrays but can''t use strlen because
it just stops counting at the first null it finds. so how to do it?

Angus

解决方案

Angus a écrit :

Hello

I have a stream of bytes - unsigned char*. But the ''string'' may contain
embedded nulls. So not like a traditional c string terminated with a null.

I need to calculate the length of these arrays but can''t use strlen because
it just stops counting at the first null it finds. so how to do it?

Angus

There is no way to do it since you have no algorithm to determine
its length.


Angus said:

Hello

I have a stream of bytes - unsigned char*. But the ''string'' may contain
embedded nulls. So not like a traditional c string terminated with a
null.

I need to calculate the length of these arrays but can''t use strlen
because
it just stops counting at the first null it finds. so how to do it?

Well, now you know what null is for. :-)

Whenever you read data, you need to establish a protocol for stopping. If
you''re reading a text file, typically you stop (or at least pause for
thought) when you hit a newline. If you''re reading an email feed, you stop
when you get ".\r\n". If you''re copying a string, you stop at the null
terminator. All of these are termination protocols.

Clearly, you need a terminating protocol, too. If no particular value (''\0'',
''\n'') or combination of values (".\r\n") suggests itself as a sentinel,
then you have little option but to insist that your data feed is
accompanied by relevant information regarding its length.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.


Angus wrote:

Hello

I have a stream of bytes - unsigned char*. But the ''string'' may contain
embedded nulls. So not like a traditional c string terminated with a null.

I need to calculate the length of these arrays but can''t use strlen because
it just stops counting at the first null it finds. so how to do it?

If this stream is of a specific format and has the length embedded in
it, you can extract it. How to do this depends on the format.
Otherwise, if the length is not kept elsewhere, you need to keep track
of it yourself.


这篇关于计算嵌入空值的字节字符串的长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆