简体   繁体   中英

Filter trash from Byte[]

I want to filter out a lot of trash from my Byte[]. but I don't know how I should do it.

When I convert the Byte[] to a string, the output is the following:

[00][01][00][00][00]ÿÿÿÿ[01][00][00][00][00][00][00][00][0F][01][00][00][00][05][00][00][00][02][02]PÑ[0B][00][01][00][00][00]ÿÿÿÿ[01][00][00][00][00][00][00][00][0F][01][00][00][00][02][00][00][00][02][03][15][0B][00][01][00][00][00]ÿÿÿÿ[01][00][00][00][00][00][00][00][0F][01][00][00][00][02][00][00][00][02][02][0B][00][01][00][00][00]ÿÿÿÿ[01][00][00][00][00][00][00][00][0F][01][00][00][00][04][00][00][00][02]PÑ[03][0B][00][01][00][00][00]ÿÿÿÿ[01][00][00][00][00][00][00][00][0F][01][00][00][00][04][00][00][00][02][02]PÑ[0B][00][01][00][00][00]ÿÿÿÿ[01][00][00][00][00][00][00][00][0F][01][00][00][00][02][00][00][00][02][03][0B][00][01][00][00][00]ÿÿÿÿ[01][00][00][00][00][00][00][00][0F][01][00][00][00][06][00][00][00][02][02]PÑ[03][0B][00][01][00][00][00]ÿÿÿÿ[01][00][00][00][00][00][00][00][0F][01][00][00][00][01][00][00][00][02][0B][00][01][00][00][00]ÿÿÿÿ[01][00][00][00][00][00][00][00][0F][01][00][00][00][05][00][00][00][02][02]PÑ[03][0B][00][01][00][00][00]ÿÿÿÿ[01][00][00][00][00][00][00][00][0F][01][00][00][00][06][00][00][00][02][02]PÑ[03][0B][00][01][00][00][00]ÿÿÿÿ[01][00][00][00][00][00][00][00][0F][01][00][00][00][01][00][00][00][02][15][0B][00][01][00][00][00]ÿÿÿÿ[01][00][00][00][00][00][00][00][0F][01][00][00][00][02][00][00][00][02][02][0B][00][01][00][00][00]ÿÿÿÿ[01][00][00][00][00][00][00][00][0F][01][00][00][00]([00][00][00][02]PB000A07B;1359120102;NL1200000002 ;w4[03][0B][00][01][00][00][00]ÿÿÿÿ[01][00][00][00][00][00][00][00][0F][01][00][00][00][06][00][00][00][02][02]PB01[0B][00][01][00][00][00]ÿÿÿÿ[01][00][00][00][00][00][00][00][0F][01][00][00][00]$[00][00][00][02]4EC8F;1359300102;NL1200000001 ;Ey³[03][0B][00][01][00][00][00]ÿÿÿÿ[01][00][00][00][00][00][00][00][0F][01][00][00][00][08][00][00][00][02][02]PCD325[0B][00][01][00][00][00]ÿÿÿÿ[01][00][00][00][00][00][00][00][0F][01][00][00][00]"[00][00][00][02]973;1325301402;NL1200000004 ;AOl[03][0B][00][01][00][00][00]ÿÿÿÿ[01][00][00][00][00][00][00][00][0F][01][00][00][00][03][00][00][00][02][02]P[0B][00][01][00][00][00]ÿÿÿÿ[01][00][00][00][00][00][00][00][0F][01][00][00][00][03][00][00][00][02]Ñ[03][0B][00][01][00][00][00]ÿÿÿÿ[01][00][00][00][00][00][00][00][0F][01][00][00][00][03][00][00][00][02][02]P[0B][00][01][00][00][00]ÿÿÿÿ[01][00][00][00][00][00][00][00][0F][01][00][00][00][03][00][00][00][02]Ñ[03][0B]

That is you can see a lot, I want to filter out the following: [00] , [0F] , ÿÿÿÿ and [01] .

How do I filter this string that way? And if possible, how do I filter the Byte[] direct without converting to string first? Because that would help me a lot if there is a way to filter the byte[] direct from the following items: [00] , [0F] , ÿÿÿÿ and [01] .

Thanks in Advance

How do I filter this string that way?

You can use this regex:

string filteredStr = Regex.Replace(strWithTrash, @"\[00\]|\[01\]|\[0F\]|(ÿÿÿÿ)", "");
// now, you can convert your filtered string back to a byte array

And if possible, how do I filter the Byte[] direct without converting to string first?

For that, you could use loops, and looking at the char code of [ , 0 , 1 , F , ] and ÿ , but it is a lot easier to just convert it to a string and remove the trash.

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