简体   繁体   English

C# 从字节数组中获取第一个字节索引

[英]C# get first byte index from a byte array

I have a byte Array that contains the char '%' (25 hex, 37 decimal).我有一个字节数组,其中包含字符“%”(25 十六进制,37 十进制)。 I would like to get the index of this byte, however, none of these methods work, and -1 is returned.我想获取这个字节的索引,但是,这些方法都不起作用,并且返回 -1。 How to get the index of a specific byte in a byte array?如何获取字节数组中特定字节的索引?

int byteIndex = Array.IndexOf(bytesDataArray, '%');
int byteIndex = Array.IndexOf(bytesDataArray, 37);
int byteIndex = Array.IndexOf(bytesDataArray, 0x25);

You're trying to find an int value of 37 in a byte array.您试图在字节数组中找到 37 的 int 值。 Byte arrays don't contain ints.字节 arrays 不包含整数。 You need to do a type cast:您需要进行类型转换:

int byteIndex = Array.IndexOf(bytesDataArray, (byte)37);

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

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