简体   繁体   English

如何使用 C 检查字节数组中的字节是否包含 0?

[英]How to check if a byte from a byte array contains 0 using C?

I have a byte array of strings .我有一个stringsbyte array How can I check if a byte in that array contains 0?如何检查该数组中的字节是否包含 0? For example:例如:

char byte_arr[5] = "abc00";

The last two bytes of the above array contains 0. How can I detect that in c ?上述数组的最后两个字节包含 0。如何在c中检测到?

Simple algorithm to follow遵循的简单算法

count_zero = 0
for each element a, in byte_array
    check if a is '0'
        count_zero++  

Even if we make this code compile (assuming that uint8 is defined somewhere)即使我们编译这段代码(假设uint8是在某处定义的)

uint8 byte_arr[5] = "abc00";

then no bytes in this array (except the 5th one) has value zero.那么这个数组中的任何字节(第 5 个除外)的值都为零。 Last two hold value of the character '0' which depends of the character encoding used.最后两个保持字符'0'值,这取决于所使用的字符编码。

To check if a particular element of the array holds some value you need to compare it with that value要检查数组的特定元素是否包含某个值,您需要将其与该值进行比较

if(byte_arr[4] == '0') printf("Success!!!!\n");
else printf("Failure!!!!\n");

For starters your character array does not contain a string.对于初学者,您的字符数组不包含字符串。

char byte_arr[5] = "abc00";

So to determine whether the character '0' is present in the array you have to traverse it elements using its size.因此,要确定字符“0”是否存在于数组中,您必须使用它的大小遍历它的元素。

In this case you can use a loop as for example while loop在这种情况下,您可以使用循环,例如 while 循环

size_t i = 0;
char c = '0';

while ( i < 5 && byte_arr[i] != c ) i++;

if ( i != 5 ) printf( "The character %c is present at position %zu\n, c, i );

If you need to determine positions of all elements that contain the character '0' or count them then you can use for example a for loop.如果您需要确定包含字符'0'的所有元素的位置或计算它们,那么您可以使用例如 for 循环。

#include <stdio.h>

int main(void) 
{
    enum { N = 5 };
    char byte_arr[N] = "abc00";
    char c = '0';
    
    size_t count = 0;
    
    for ( size_t i = 0; i < N; i++ )
    {
        if ( byte_arr[i] == c )
        {
            ++count;
            printf( "At the position %zu there is the character '%c'\n", i, c );
        }
    }
    
    printf( "There are %zu elements in the array that contains '%c'.\n", count, c );
    
    return 0;
}

The program output is程序 output 是

At the position 3 there is the character '0'
At the position 4 there is the character '0'
There are 2 elements in the array that contains '0'.

If you would declare the array for example like如果您要声明数组,例如

char byte_arr[] = "abc00";

then the array will conmtain a string.那么数组将包含一个字符串。 In this case you can use the standard string function strchr that checks whether a given character is present in a string.在这种情况下,您可以使用标准字符串 function strchr检查字符串中是否存在给定字符。 For example例如

#include <string.h>

//...

char byte_arr[] = "abc00";
char c = '0';

char *p = strchr( byte_arr, c );

if ( p != NULL ) printf( "The character %c is present at position %zu\n, c, ( size_t )( p - byte_arr ) );
 

There's a standard function for this, in string.h , if the task is just to determine whether or not there are any:string.h中有一个标准的 function ,如果任务只是确定是否有:

if ( memchr(byte_arr, '0', sizeof byte_arr) )
    printf("There was a zero.\n");

You can loop the array until you reach the end or find the desired char:您可以循环数组直到到达末尾或找到所需的字符:

int main(void) {
    char byte_arr[5] = "abc00";
    for (int i =0; i<5; ++i)
    {
        if(byte_arr[i]=='0'){
            printf("Found!!");
            break;
        }
    }
    return 0;
}

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

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