简体   繁体   中英

Comparing byte array hex values in c#

I am making C# windows application.In that application i have one byte array containing hex values.suppose i have byte array as

 array[0]=4E
 array[1]=50

Here i want to combination of these values as 4E50 to compare.I dont want to compare single byte.Please help me.Thanks in advance.

I'm not sure if I understand correctly do you want to compare 0x4E50 with another number? If so you can do as follows:

(array[0] << 8) + array[1]

This will give you 0x4E50.

StringBuilder sb = new StringBuilder();
foreach (byte b in array)
{
       sb.AppendFormat("{0:x2}", b)
}
return sb.ToString();

edit

inspired by Godwin solution , but better :

(array[0] << 8) | array[1]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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