本文介绍了对于二进制和ASCII数据,strlen是否返回相同的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请找到以下代码段.

unsigned char bInput[20];
unsigned char cInput[20];

从一个函数中,我在bInput中获取一个二进制数据,并使用strlen(bInput)确定了它的长度.我将二进制形式的bInput转换为ASCII,并存储在cInput中,并打印了其长度.但是两者是不同的.

From a function, I get a binary data in bInput and I determined its length using strlen(bInput).I converted bInput which is in binary to ASCII and stored in cInput and printed its length. But both are different.

我是编程新手.请对其行为进行指导.

I am new to programming. Please guide regarding its behaviour.

推荐答案

结果在两种情况下都不相同.以下是一个示例方案:Null是有效的UTF-8,仅对C'strings'无效.

result wont be same for both cases.Below is one sample scenario:Null is valid UTF-8, it just doesn't work with C 'strings'.

   char temp[8];
   buf = "abcde\0f";

这里有一个长度为8的缓冲区,其中包含以下char值:

What we have here is a buffer of length 8, which contains these char values:

97 98 99 100 101 0 102 0

在这里,按照strlen设计,strlen(temp)等于5,但是,缓冲区的实际长度是8.

here,strlen(temp) is equal to 5 as per strlen design,however,The actual length of the buffer is eight.

这篇关于对于二进制和ASCII数据,strlen是否返回相同的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 13:10