Author Topic: bytes, strings bytearrays???  (Read 2255 times)

0 Members and 1 Guest are viewing this topic.

Offline lkj

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 485
  • Rating: +58/-1
    • View Profile
bytes, strings bytearrays???
« on: March 16, 2012, 06:32:09 pm »
I really don't get why this python 3.2 code doesn't work:
Code: [Select]
file = open("evil2.gfx","rb")
tmp = bytearray(file.read())
file.close()
tmp1 = b''
tmp2 = b''
tmp3 = b''
tmp4 = b''
tmp5 = b''
result = b''
for i in range(0,len(tmp)):
    if i%5 == 0:
        tmp1 += bytes(tmp[i])
    elif i%5 == 1:
        tmp2 += bytes(tmp[i])
    elif i%5 == 2:
        tmp3 += bytes(tmp[i])
    elif i%5 == 3:
        tmp4 += bytes(tmp[i])
    elif i%5 == 4:
        tmp5 += bytes(tmp[i])

I really don't understand why I can't add a single byte from a bytestring to a bytestring without typecasting, and if I try to convert it like above with bytes() I get very long bytestrings only containing null ???
Is the typesystem of Python really that much more difficult than that of C or do I just not understand it yet?