python - how to check for a presence of a character in a byte array -


this sounds basic me ask here goes. have bytearray. want check presence of let's 'a' or 'a' in array , print count of them. following don't see though know there 'a' in there -

a_bytes = bytearray.fromhex(hex_string) count = 0 x in a_bytes:     if ( (x=='a') or (x == 'a') ):         count = count+1  return count 

why doesn't above code work? printed out byte values integers , see 65 repeating multiple times. again try convert constant 'a' integer using int('a') error --

valueerror: invalid literal int() base 10: 'a' 

the values in bytearray stored integers, not hex representations. need search 65 or 97, not "a" or "a".

if want use strings, use list. if you're not interested in integer values of bytes, bytearray not right choice. also, if use list, can use .count method of lists directly count occurrences of particular value.


Comments

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -