简体   繁体   中英

length of array returns wrong value when including 0x00 hex number

This is the code that I am running (using Python 3.7):

barray = {
0x22, 0x00, 0x43, 0x61, 0x62, 0x74, 0x72, 0x6f,
0x6e, 0x69, 0x78, 0x00, 0x01, 0x00, 0x41, 0x54,
0xff, 0xff, 0xff, 0xff
}
asc = '"  C a b C a b n i x    A T ÿ ÿ ÿ ÿ'


def hex_to_uint(hexarray):
    length = len(hexarray)
    rows = (length-4)/8
    print(length)
    print(rows)
    # todo write rest of function

What I respected was to have the numbers 20 and 2 to be printed on screen. However I get 15 and 1.375.

I think it has to do with the number 0x00 in the array, but I dont know.

You seem to create a set() rather than a list() . Lists in python are created using square brackets like so: [1, 2, 3] . A set will always get rid of duplicate elements in it. You can read up on the built-in types here .

I assume this relates to your error, since you called it an 'array', which it is not. Change the brackts and re-run your code to see if it works.

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